PHP sample code for collecting remote images

Source: Internet
Author: User
Tags php sample code preg
PHP sample code for collecting remote images

  1. Function make_dir ($ path ){
  2. If (! File_exists ($ path) {// creation if the object does not exist
  3. $ Mk = @ mkdir ($ path, 0777); // permission
  4. @ Chmod ($ path, 0777 );
  5. }
  6. Return true;
  7. }

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

  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. }

The write_filetext () function writes the image content fputs into the file to save the image file.

  1. Function write_filetext ($ filepath, $ string ){
  2. // $ String = stripSlashes ($ string );
  3. $ Fp = @ fopen ($ filepath, "w ");
  4. @ Fputs ($ fp, $ string );
  5. @ Fclose ($ fp );
  6. }

The get_filename () function is used to obtain the image name. you can also customize the file name to save.

  1. Function get_filename ($ filepath ){
  2. $ Fr = explode ("/", $ filepath );
  3. $ Count = count ($ fr)-1;
  4. Return $ fr [$ count];
  5. }

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

  1. Function save_pic ($ url, $ savepath = ''){
  2. // Processing address
  3. $ Url = trim ($ url );
  4. $ Url = str_replace ("", "% 20", $ url );
  5. // Read the file
  6. $ String = read_filetext ($ url );
  7. If (empty ($ string )){
  8. Echo 'file not readable '; 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 an object
  17. Write_filetext ($ filepath, $ string );
  18. Return $ filepath;
  19. }

In the last step, 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 the Directory
  4. $ Savepath = "images /";
  5. Echo save_pic ($ pic, $ savepath );

In practice, the content of a website, such as product information, may be collected, including anti-Leech images, and stored on the website server. At this time, you can use the regular expression matching page content to find the matched images on the page, and then download them to the website server to complete image collection.

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]; // Obtain the image array
  5. Foreach ($ pic_arr as $ pic_item) {// cyclically retrieve the address of each graph
  6. Save_pic ($ pic_item, $ path); // download and save the image
  7. Echo "[OK]...! ";
  8. }
  9. }

Then, analyze the page content, find the subject content, and call get_pic () to save the image.

  1. // Collect the picture on the previous mobile phone report page of the Pacific Computer Network
  2. $ Url = "http://gz.pconline.com.cn/321/3215791.html ";
  3. $ Content = file_get_contents ($ url); // Obtain the webpage content
  4. $ Preg = '#

    (.*)

    # IUs ';
  5. Preg_match_all ($ preg, $ content, $ arr );
  6. $ Cont = $ arr [1] [0];
  7. Get_pic ($ cont, 'IMG /');

The above code can be used to collect images, but some scenarios have not been taken into account. for example, if the target website has been redirected for more than 302 times, the target website has been protected against multiple types of attacks, you can study it on your own.

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.