Php remote website image capture and save code

Source: Internet
Author: User
Php remote website image capture and save code

Example: php code for capturing website data.

  1. /**
  2. * A category used to capture images
  3. *
  4. * @ Package default
  5. * @ Author WuJunwei
  6. */
  7. Class download_image
  8. {
  9. Public $ save_path; // save the image capture address
  10. // The size limit of the captured image (unit: Byte) is greater than the size limit.
  11. Public $ img_size = 0;
  12. // Define a static array to record the URL of a previously crawled hyperlink to avoid repeated capturing
  13. Public static $ a_url_arr = array ();
  14. /**
  15. * @ Param String $ save_path: address for saving the captured image
  16. * @ Param Int $ img_size: address for saving the captured image
  17. */
  18. Public function _ construct ($ save_path, $ img_size)
  19. {
  20. $ This-> save_path = $ save_path;
  21. $ This-> img_size = $ img_size;
  22. }
  23. /**
  24. * Recursive method for downloading and capturing pictures on the home page and its subpages (recursive recursion)
  25. *
  26. * @ Param String $ capture_url the URL used to capture the image
  27. *
  28. */
  29. Public function recursive_download_images ($ capture_url)
  30. {
  31. If (! In_array ($ capture_url, self: $ a_url_arr) // never captured
  32. {
  33. Self: $ a_url_arr [] = $ capture_url; // included in the static array
  34. } Else // crawled and exited the function directly
  35. {
  36. Return;
  37. }
  38. $ This-> download_current_page_images ($ capture_url); // download all images on the current page
  39. // Use @ to block the warning error caused by the unreadable captured address.
  40. $ Content = @ file_get_contents ($ capture_url );
  41. // Match the href attribute of tag? Previous regular expressions
  42. $ A_pattern = "|] + href = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
  43. Preg_match_all ($ a_pattern, $ content, $ a_out, PREG_SET_ORDER );
  44. $ Tmp_arr = array (); // defines an array to store the hyperlink address for capturing images in the current loop.
  45. Foreach ($ a_out as $ k => $ v)
  46. {
  47. /**
  48. * Remove the null '', '#', '/' and repeated values in the hyperlink.
  49. * 1: The value of the hyperlink address cannot be equal to the url of the currently crawled page. Otherwise, the url will be in an endless loop.
  50. * 2: The hyperlink is ''or '#', and '/' is also the current page, which will also fall into an endless loop,
  51. * 3: sometimes a hyperlink address appears multiple times on a webpage. if this address is not removed, a subpage is repeatedly downloaded)
  52. */
  53. If ($ v [1] &! In_array ($ v [1], self: $ a_url_arr )&&! In_array ($ v [1], array ('#', '/', $ capture_url )))
  54. {
  55. $ Tmp_arr [] = $ v [1];
  56. }
  57. }
  58. Foreach ($ tmp_arr as $ k => $ v)
  59. {
  60. // URL of the hyperlink
  61. If (strpos ($ v, 'http ://')! = False) // if the url contains http: //, you can directly access
  62. {
  63. $ A_url = $ v;
  64. } Else // otherwise, it indicates the relative address. you need to splice the hyperlink's access address.
  65. {
  66. $ Domain_url = substr ($ capture_url, 0, strpos ($ capture_url, '/', 8) + 1 );
  67. $ A_url = $ domain_url. $ v;
  68. }
  69. $ This-> recursive_download_images ($ a_url );
  70. }
  71. }
  72. /**
  73. * Download all images on the current webpage
  74. *
  75. * @ Param String $ capture_url the webpage address used to capture images
  76. * @ Return Array an Array of the URLs of all img tags on the current webpage
  77. */
  78. Public function download_current_page_images ($ capture_url)
  79. {
  80. $ Content = @ file_get_contents ($ capture_url); // block warning errors
  81. // Matching the src attribute of the img label? Previous regular expressions
  82. $ Img_pattern = "|] + src = ['\"]? ([^ '\ "?] +) ['\ ">] | U ";
  83. Preg_match_all ($ img_pattern, $ content, $ img_out, PREG_SET_ORDER );
  84. $ Photo_num = count ($ img_out );
  85. // Number of matched images
  86. Echo ''. $ capture_url." found ". $ photo_num." images ";
  87. Foreach ($ img_out as $ k => $ v)
  88. {
  89. $ This-> save_one_img ($ capture_url, $ v [1]);
  90. }
  91. }
  92. /**
  93. * How to save a single image
  94. *
  95. * @ Param String $ capture_url the webpage address used to capture images
  96. * @ Param String $ img_url the url of the image to be saved
  97. *
  98. */
  99. Public function save_one_img ($ capture_url, $ img_url)
  100. {
  101. // Image path
  102. If (strpos ($ img_url, 'http ://')! = False)
  103. {
  104. // $ Img_url = $ img_url;
  105. } Else
  106. {
  107. $ Domain_url = substr ($ capture_url, 0, strpos ($ capture_url, '/', 8) + 1 );
  108. $ Img_url = $ domain_url. $ img_url;
  109. }
  110. $ Pathinfo = pathinfo ($ img_url); // Obtain the image path information
  111. $ Pic_name = $ pathinfo ['basename']; // Obtain the image name.
  112. If (file_exists ($ this-> save_path. $ pic_name) // if the image exists, it indicates that it has been crawled and exits the function.
  113. {
  114. Echo $ img_url. 'the image has been captured!
    ';
  115. Return;
  116. }
  117. // Read the image content into a string
  118. $ Img_data = @ file_get_contents ($ img_url); // block the warning error caused by the inability to read the image address.
  119. If (strlen ($ img_data)> $ this-> img_size) // download an image with a larger size ratio.
  120. {
  121. $ Img_size = file_put_contents ($ this-> save_path. $ pic_name, $ img_data );
  122. If ($ img_size)
  123. {
  124. Echo $ img_url. 'the image is saved successfully!
    ';
  125. } Else
  126. {
  127. Echo $ img_url. 'an error occurred while saving the image!
    ';
  128. }
  129. } Else
  130. {
  131. Echo $ img_url. 'reading the image failed!
    ';
  132. }
  133. }
  134. } // END
  135. Set_time_limit (120); // sets the maximum execution time of the script based on actual conditions.
  136. $ Download_img = new download_image ('E:/images/', 0); // instantiate the downloaded image object
  137. $ Download_img-> recursive_download_images ('http: // bbs.it-home.org/'); // recursive image capturing method
  138. // $ Download_img-> download_current_page_images ($ _ POST ['capture _ url']); // only capture the image of the current page
  139. ?>

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.