PHP Remote Picture Collection Detailed Tutorial

  • 2021-07-06 10:23:01
  • OfStack

When we need to collect the content of a web page on the network, if the pictures on the target website have an anti-theft chain, the pictures we directly collect are unavailable on our own website. Then we use the program to download the pictures from the target website to our website server, and then we can call the pictures.

This paper will use PHP to realize the function of collecting remote pictures. Basic process:
1. Get the picture address of the target website.
2. Read the picture content.
3. Create the path to save the picture and name the picture.
4. Write the picture content.
5. Done.
We implement this 1 procedure by writing several functions.
The function make_dir () establishes the directory. It is judged whether a picture file directory to be saved exists, if it does not exist, a directory is created, and the directory is set to writable permission.

function make_dir($path){ 
    if(!file_exists($path)){// Establish if it does not exist
        $mk=@mkdir($path,0777); // Authority
        @chmod($path,0777);
    }
    return true;
}

The function read_filetext () gets the picture content. Use fopen to open the picture file, and then fread to read the contents of the picture file.
 
function read_filetext($filepath){
    $filepath=trim($filepath);
    $htmlfp=@fopen($filepath,"r");
    // Remote
    if(strstr($filepath,"://")){
        while($data=@fread($htmlfp,500000)){
            $string.=$data;
        }
    }
    // Local
    else{
        $string=@fread($htmlfp,@filesize($filepath));
    }
    @fclose($htmlfp);
    return $string;
}

The function write_filetext () writes the file, writes the picture content fputs in the file, namely saves the picture file.
 
function write_filetext($filepath,$string){
    //$string=stripSlashes($string);
    $fp=@fopen($filepath,"w");
    @fputs($fp,$string);
    @fclose($fp);
}

The function get_filename () gets the image name, or you can customize the file name to save.
 
function get_filename($filepath){
    $fr=explode("/",$filepath);
    $count=count($fr)-1;
    return $fr[$count];
}

Then combine several functions and call them in the function save_pic (), and finally return the saved picture path.
 
function save_pic($url,$savepath=''){
    // Processing address
    $url=trim($url);
    $url=str_replace(" ","%20",$url);
    // Read a file
    $string=read_filetext($url);
    if(empty($string)){
        echo ' Unable to read file ';exit;
    }
    // Filename
    $filename = get_filename($url);
    // Storage directory
    make_dir($savepath); // Establish a storage directory
    // File address
    $filepath = $savepath.$filename;
    // Write a file
    write_filetext($filepath,$string);
    return $filepath;
}

The last step is to call the save_pic () function to save the picture, and we test it with the following code.
 
// Destination picture address
$pic = "http://img0.pconline.com.cn/pconline/1205/06/2776119_end1_thumb.jpg";
// Save directory
$savepath = "images/";
echo save_pic($pic,$savepath);

In practical application, we may collect the content of a certain site, such as product information, including collecting the pictures of anti-theft chain and saving them to the server on the website. At this time, we can use regular matching page content, find out the matching pictures in the page, and then download them to the website server to complete the picture collection. The following code is for testing only:
 
function get_pic($cont,$path){
    $pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/';
    $num = preg_match_all($pattern_src, $cont, $match_src);
    $pic_arr = $match_src[1]; // Get an array of pictures
    foreach ($pic_arr as $pic_item) { // Loop out the address of each picture
        save_pic($pic_item,$path); // Download and save pictures
        echo "[OK]..!";
    }
}

Then we find out the main content by analyzing the page content, and call get_pic () to save the picture.
 
// We collect Pacific computer network 1 A picture of the content page of the mobile phone report
$url = "http://gz.pconline.com.cn/321/3215791.html";
 
$content = file_get_contents($url);// Getting Web Content
$preg = '#<div class="art_con">(.*)<div class="ivy620 ivy620Ex"></div>#iUs';
preg_match_all($preg, $content, $arr);
$cont = $arr[1][0]; 
get_pic($cont,'img/');

The above code is personally tested by the author, and pictures can be collected, but there are still some scenes that have not been taken into account. For example, the target website has made more than 302 jumps, and the target website has made a variety of anti-collection, so leave it to the students who like tossing and turning.


Related articles: