Using PHP to capture remote images

Source: Internet
Author: User
Tags count fread functions preg return trim website server

When we need to collect a Web page content on the Web, if the image on the target site to do the anti-theft chain, we collected directly from the picture on their own website is not available. Then we use the program to download the image on the target website to our website server, then we can call the picture.

This article will use PHP to achieve the collection of remote image features. Basic process:

1, get the target website picture address.

2, read the picture content.

3, create the path to save the picture and name the picture.

4, write the picture content.

5, complete.

We do this by writing a few functions.

function Make_dir () to create a directory. Determine if the picture file directory you want to save exists, create a directory if it does not exist, and set the directory to write permissions.


function Make_dir ($path) {
if (!file_exists ($path)) {//non-existent
$MK = @mkdir ($path, 0777);//Permissions
@chmod ($path, 0777);    
} 
return true;
}

function Read_filetext () Gets the picture content. Use fopen to open the picture file, and then fread read the picture file contents.


function Read_filetext ($filepath) {
$filepath =trim ($filepath);
$HTMLFP = @fopen ($filepath, "R");
//Remote
if (strstr ($filepath, "://")) {while
($data = @fread ($HTMLFP, 500000)) {
$st Ring.= $data;    
} 
}
//local
else{
$string = @fread ($HTMLFP, @filesize ($filepath));
}
@fclose ($HTMLFP);
return $string;
}

The function Write_filetext () writes the file, writes the picture content fputs to the file, namely saves the picture file.


function Write_filetext ($filepath, $string) {
//$string =stripslashes ($string);
$fp = @fopen ($filepath, "w");    
@fputs ($fp, $string);
@fclose ($FP);
}

function Get_filename () Gets the name of the picture, or you can customize the name of the file you want to save.


function Get_filename ($filepath) {
$fr =explode ("/", $filepath);
$count =count ($FR)-1; return
$FR [$count];
}

Several functions are then combined, called in the function Save_pic (), and finally the saved picture path is returned.

   Function save_pic ($url, $savepath = ') {      //processing address         $url =trim ($url);       $url =str_replace (" ", "%20", $url);       //read files        $string =read_filetext ($url);     
 if (Empty ($string)) {          echo  ' can't read file ';exit;      }      //filename        $filename  =  get_filename ($url);      //storage directory       make_dir ($savepath)  //set up storage directory       //file address        $filepath  = $ Savepath. $filename;      //Write files       write_filetext ($filepath, $ String);      return  $filepath; }  

The final step is to call the Save_pic () function to save the picture, and we'll use the following code to do the test.


//target picture address
$pic = "/program/uploadpic/2013-4/201343155341353.jpg";
Save directory
$savepath = "images/";
Echo save_pic ($pic, $savepath);

In practice, we may collect the content of a certain site, such as product information, including the collection of photos to save the chain to the Web server. At this time we can use the regular matching page content, the pages of matching pictures are found, and then downloaded to the Web server, complete the collection of pictures. The following code is for testing only:


function Get_pic ($cont, $path) {
$pattern _src = '/<[imgimg].*?src=&/.jpg])) [\'\"].*? [\/]?>/';    
$num = Preg_match_all ($pattern _src, $cont, $match _src);
$pic _arr = $match _src[1];        Get picture array
foreach ($pic _arr as $pic _item) {//loop out the address of each map
save_pic ($pic _item, $path);//download and save pictures
echo "[ok]..!";
} 
}

Then we analyze the content of the page, find out the main content, call Get_pic () to achieve the preservation of the picture.


//We collect Pacific Internet on a mobile phone coverage page of the picture
$url = "http://gz.pconline.com.cn/321/3215791.html";
$content = file_get_contents ($url);//Get Web page 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 to test, you can collect pictures, but still some scenes did not take into account, such as the target site did more than 302 jumps, the target site did a variety of anti-collection, left to like tossing students to try it.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.