Using PHP to capture remote images

Source: Internet
Author: User
Tags fread preg 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 directly collected pictures on their own site 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 remote image capture function. Basic flow:   1, get the target site image address.   2, read the picture content.   3, create a 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)) {//No existence is established   $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"); &N   Bsp   Remote   if (Strstr ($filepath, "://")) {  while ($data = @fread ($HTMLFP, 500000)) {  $string. = $data;   { }  /local   else{  $string = @fread ($HTMLFP, @filesize ($filepath));  }   @fcl OSE ($HTMLFP);   return $string;  }   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, $str ing);   @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   RET Urn $fr [$count];  }   then combines several functions, called in function Save_pic (), and finally returns the saved picture path.   function Save_pic ($url, $savepath = ') { //processing address   $url =trim ($url);   $url =str_replace ("", "%2 0 ", $url);  /Read files   $string =read_filetext ($url);   if (empty ($string)) {  echo ' cannot read file '; exit  }  //filename   $filename = get_filename ($url );  //Storage directory   Make_dir ($savepath); Create a storage directory  //file address   $filepath = $savepath. $filename;  //write file   Write_filetext ($filepath, $string);   return $filepath;     The last 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 practical applications, we may collect the content of a site, such as product information, including the collection of anti-theft chain of pictures saved to the site on the 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=/picture/allimg/130409/123 450bk-0.gif/.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   save_pic of each map ($pic _item, $path);//download and save pictures   EC Ho "[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 collected Pacific Internet 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);//Get 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 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.