Home > Backend Development > PHP Tutorial > PHP example code for collecting remote images

PHP example code for collecting remote images

WBOY
Release: 2016-07-25 08:55:37
Original
872 people have browsed it
  1. function make_dir($path){
  2. if(!file_exists($path)){//Create if it does not exist
  3. $mk=@mkdir($path,0777); //Permissions
  4. @chmod($path,0777);
  5. }
  6. return true;
  7. }
Copy the code

The function read_filetext() gets the image content. Use fopen to open the image file, and then fread to read the image file content.

  1. function read_filetext($filepath){
  2. $filepath=trim($filepath);
  3. $htmlfp=@fopen($filepath,"r");
  4. //remote
  5. if( strstr($filepath,"://")){
  6. while($data=@fread($htmlfp,500000)){
  7. $string.=$data;
  8. }
  9. }
  10. //local
  11. else{
  12. $ string=@fread($htmlfp,@filesize($filepath));
  13. }
  14. @fclose($htmlfp);
  15. return $string;
  16. }
Copy code

Function write_filetext() writes the file and writes the image The content fputs is written into the file, that is, the image file is saved.

  1. function write_filetext($filepath,$string){
  2. //$string=stripSlashes($string);
  3. $fp=@fopen($filepath,"w");
  4. @ fputs($fp,$string);
  5. @fclose($fp);
  6. }
Copy code

The function get_filename() gets the image name, and you can also customize the file name to be saved.

  1. function get_filename($filepath){
  2. $fr=explode("/",$filepath);
  3. $count=count($fr)-1;
  4. return $fr[$ count];
  5. }
Copy the code

Then, combine several functions, call them in the function save_pic(), and finally return the saved picture path.

  1. function save_pic($url,$savepath=''){
  2. //Processing address
  3. $url=trim($url);
  4. $url=str_replace(" ","% 20",$url);
  5. //Read file
  6. $string=read_filetext($url);
  7. if(empty($string)){
  8. echo 'Cannot read file';exit;
  9. }
  10. //File name
  11. $filename = get_filename($url);
  12. //Storage directory
  13. make_dir($savepath); //Create a storage directory
  14. //File address
  15. $filepath = $savepath.$filename;
  16. //Write a file
  17. write_filetext( $filepath,$string);
  18. return $filepath;
  19. }
Copy the code

The last step is to call the save_pic() function to save the image, and use the following code for testing.

  1. //Target image address
  2. $pic = "http://img0.pconline.com.cn/pconline/1205/06/2776119_end1_thumb.jpg";
  3. //Save directory
  4. $savepath = "images/";
  5. echo save_pic($pic,$savepath);
Copy code

In actual application, the content of a certain site may be collected, such as product information, including collection of anti-hotlinking information The image is saved to the server on the website. At this time, you can use regular matching to match the page content, find all the matching pictures in the page, and then download them to the website server respectively to complete the collection of pictures.

Test example:

  1. function get_pic($cont,$path){
  2. $pattern_src = '/<[img|IMG].*?src=['|"](.*?(? :[.gif|.jpg]))['|"].*?[/]?>/';
  3. $num = preg_match_all($pattern_src, $cont, $match_src);
  4. $pic_arr = $match_src[ 1]; //Get the picture array
  5. foreach ($pic_arr as $pic_item) { //Loop to get the address of each picture
  6. save_pic($pic_item,$path); //Download and save the picture
  7. echo "[OK]. .!";
  8. }
  9. }
Copy the code

Then, by analyzing the page content, find out the main content, and call get_pic() to save the picture.

  1. //Collect pictures of the content page of a report about mobile phones on Pacific Computer Network
  2. $url = "http://gz.pconline.com.cn/321/3215791.html";
  3. $content = file_get_contents($url);//Get web content
  4. $preg = '#
    (.*)
    #iUs';
  5. preg_match_all($preg, $content, $arr);
  6. $cont = $arr[1][0];
  7. get_pic($cont,'img/');
Copy code

The above code was personally tested by the author, and it can collect images, but there are still some scenarios that have not been taken into account. For example, the target website has made more than 302 jumps, and the target website has made various anti-collection methods. Please study it yourself.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template