search
HomeBackend DevelopmentPHP TutorialTutorial on capturing remote images with PHP

When I was doing WeChat login development before, I found that WeChat avatar pictures did not have a suffix name. The traditional image capture method did not work and required special capture processing. Therefore, various situations were later combined, encapsulated into a class, and shared.

Create project

As a demonstration, we create the project grabimg in the www root directory, create a class GrabImage.php and an index.php.

Write class code

We define a class with the same name as the file: GrabImage

class GrabImage{}

Attributes

Next define several attributes that need to be used.

1. First define an image address that needs to be captured: $img_url

2. Then define a $file_name to store the name of the file, but does not carry the extension name, because it may involve extension name replacement, so I will unpack it here. Definition

3, followed by the extension name $extension

4, and then we define a $file_dir. The function of this attribute is to store the directory where the remote image is captured to the local, generally relative to the location of the PHP entry file. as a starting point. However, this path is generally not saved to the database.

5. Finally, we define a $save_dir. As the name suggests, this path is the directory of the database used to save directly. To explain here, we do not directly store the file saving path in the database. This is usually to prepare for the convenience of changing the path if the system is migrated later. Our $save_dir here is generally the date + file name. If you need to use it, take it out and spell the required path in front.

Method

Attributes are finished, now we officially start the crawling work.

First we define an open method getInstances to obtain some data, such as the address of the captured image and the local save path. Also put it in properties.

public function getInstances($img_url , $base_dir)
{
    $this->img_url = $img_url;
    $this->save_dir = date("Ym").'/'.date("d").'/'; // 比如:201610/19/
    $this->file_dir = $base_dir.'/'.$this->save_dir.'/'; // 比如:./uploads/image/2016/10/19/
}

The picture saving path is spliced. Now we have to pay attention to a question, whether the directory exists. The date goes by day by day, but the directory is not automatically created. Therefore, before saving the image, you need to check it first. If the current directory does not exist, we need to create it immediately.

We create the set directory method setDir. We set the attribute to private, which is safe

/**
 * 检查图片需要保持的目录是否存在
 * 如果不存在,则立即创建一个目录
 * @return bool
 */
private function setDir()
{
    if(!file_exists($this->file_dir))
    {
        mkdir($this->file_dir,0777,TRUE);
    }

    $this->file_name = uniqid().rand(10000,99999);// 文件名,这里只是演示,实际项目中请使用自己的唯一文件名生成方法

    return true;
}

The next step is to capture the core code

The first step is to solve a problem. The images we need to capture may not have a suffix name. According to the traditional crawling method, it is not feasible to crawl the image first and then intercept the suffix name.

We must get the image type through other methods. The method is to obtain the file header information from the file stream information, thereby judging the file mime information, and then you can know the file suffix name.

For convenience, first define a mime and file extension mapping.

$mimes=array(
    'image/bmp'=>'bmp',
    'image/gif'=>'gif',
    'image/jpeg'=>'jpg',
    'image/png'=>'png',
    'image/x-icon'=>'ico'
);

In this way, when I get the type of image/gif, I can know that it is a .gif picture.

Use the php function get_headers to obtain file stream header information. When its value is not false, we assign it to the variable $headers

and take out the value of Content-Type which is the value of mime.

if(($headers=get_headers($this->img_url, 1))!==false){
    // 获取响应的类型
    $type=$headers['Content-Type'];
}

Using the mapping table we defined above, we can easily obtain the suffix name.

$this->extension=$mimes[$type];

Of course, the $type obtained above may not exist in our mapping table, which means that this type of file is not what we want. Just discard it and ignore it.

The following steps are the same as traditional grabbing files.

$file_path = $this->file_dir.$this->file_name.".".$this->extension;
// 获取数据并保存
$contents=file_get_contents($this->img_url);
if(file_put_contents($file_path , $contents))
{
    // 这里返回出去的值是直接保存到数据库的路径 + 文件名,形如:201610/19/57feefd7e2a7aY5p7LsPqaI-lY1BF.jpg
    return $this->save_dir.$this->file_name.".".$this->extension;
}

First get the full path of the local saved image $file_path, then use file_get_contents to grab the data, and then use file_put_contents to save to the file path just now.

Finally we return a path that can be saved directly to the database instead of a file storage path.

The complete version of this crawling method is:

private function getRemoteImg()
{
    // mime 和 扩展名 的映射
    $mimes=array(
        'image/bmp'=>'bmp',
        'image/gif'=>'gif',
        'image/jpeg'=>'jpg',
        'image/png'=>'png',
        'image/x-icon'=>'ico'
    );
    // 获取响应头
    if(($headers=get_headers($this->img_url, 1))!==false)
    {
        // 获取响应的类型
        $type=$headers['Content-Type'];
        // 如果符合我们要的类型
        if(isset($mimes[$type]))
        {
            $this->extension=$mimes[$type];
            $file_path = $this->file_dir.$this->file_name.".".$this->extension;
            // 获取数据并保存
            $contents=file_get_contents($this->img_url);
            if(file_put_contents($file_path , $contents))
            {
                // 这里返回出去的值是直接保存到数据库的路径 + 文件名,形如:201610/19/57feefd7e2a7aY5p7LsPqaI-lY1BF.jpg
                return $this->save_dir.$this->file_name.".".$this->extension;
            }
        }
    }
    return false;
}

最后,为了简单,我们想在其他地方只要调用其中一个方法就可以完成抓取。所以,我们将抓取动作直接放入到getInstances中,在配置完路径后,直接抓取,所以,在初始化配置方法getInstances里新增代码。

if($this->setDir())
{
    return $this->getRemoteImg();
}
else
{
    return false;
}

测试

我们去刚刚创建的index.php文件内试试。

<?php
require_once &#39;GrabImage.php&#39;;
$object = new GrabImage();
$img_url = "http://www.bidianer.com/img/icon_mugs.jpg"; // 需要抓取的远程图片
$base_dir = "./uploads/image"; // 本地保存的路径
echo $object->getInstances($img_url , $base_dir);
?>

惹,的确抓取过来了

Tutorial on capturing remote images with PHP

完整代码


 * @link bidianer.com
 */
class GrabImage{

    /**
     * @var string 需要抓取的远程图片的地址
     * 例如:http://www.bidianer.com/img/icon_mugs.jpg
     * 有一些远程文件路径可能不带拓展名
     * 形如:http://www.xxx.com/img/icon_mugs/q/0
     */
    private $img_url;

    /**
     * @var string 需要保存的文件名称
     * 抓取到本地的文件名会重新生成名称
     * 但是,不带拓展名
     * 例如:57feefd7e2a7aY5p7LsPqaI-lY1BF
     */
    private $file_name;

    /**
     * @var string 文件的拓展名
     * 这里直接使用远程图片拓展名
     * 对于没有拓展名的远程图片,会从文件流中获取
     * 例如:.jpg
     */
    private $extension;

    /**
     * @var string 文件保存在本地的目录
     * 这里的路径是PHP保存文件的路径
     * 一般相对于入口文件保存的路径
     * 比如:./uploads/image/201610/19/
     * 但是该路径一般不直接存储到数据库
     */
    private $file_dir;

    /**
     * @var string 数据库保存的文件目录
     * 这个路径是直接保存到数据库的图片路径
     * 一般直接保存日期 + 文件名,需要使用的时候拼上前面路径
     * 这样做的目的是为了迁移系统时候方便更换路径
     * 例如:201610/19/
     */
    private $save_dir;

    /**
     * @param string $img_url 需要抓取的图片地址
     * @param string $base_dir 本地保存的路径,比如:./uploads/image,最后不带斜杠"/"
     * @return bool|int
     */
    public function getInstances($img_url , $base_dir)
    {
        $this->img_url = $img_url;
        $this->save_dir = date("Ym").'/'.date("d").'/'; // 比如:201610/19/
        $this->file_dir = $base_dir.'/'.$this->save_dir.'/'; // 比如:./uploads/image/2016/10/19/
        return $this->start();
    }

    /**
     * 开始抓取图片
     */
    private function start()
    {
        if($this->setDir())
        {
            return $this->getRemoteImg();
        }
        else
        {
            return false;
        }
    }

    /**
     * 检查图片需要保持的目录是否存在
     * 如果不存在,则立即创建一个目录
     * @return bool
     */
    private function setDir()
    {
        if(!file_exists($this->file_dir))
        {
            mkdir($this->file_dir,0777,TRUE);
        }

        $this->file_name = uniqid().rand(10000,99999);// 文件名,这里只是演示,实际项目中请使用自己的唯一文件名生成方法

        return true;
    }

    /**
     * 抓取远程图片核心方法,可以同时抓取有后缀名的图片和没有后缀名的图片
     *
     * @return bool|int
     */
    private function getRemoteImg()
    {
        // mime 和 扩展名 的映射
        $mimes=array(
            'image/bmp'=>'bmp',
            'image/gif'=>'gif',
            'image/jpeg'=>'jpg',
            'image/png'=>'png',
            'image/x-icon'=>'ico'
        );
        // 获取响应头
        if(($headers=get_headers($this->img_url, 1))!==false)
        {
            // 获取响应的类型
            $type=$headers['Content-Type'];
            // 如果符合我们要的类型
            if(isset($mimes[$type]))
            {
                $this->extension=$mimes[$type];
                $file_path = $this->file_dir.$this->file_name.".".$this->extension;
                // 获取数据并保存
                $contents=file_get_contents($this->img_url);
                if(file_put_contents($file_path , $contents))
                {
                    // 这里返回出去的值是直接保存到数据库的路径 + 文件名,形如:201610/19/57feefd7e2a7aY5p7LsPqaI-lY1BF.jpg
                    return $this->save_dir.$this->file_name.".".$this->extension;
                }
            }
        }
        return false;
    }
}


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools