PHP web image capture script

Source: Internet
Author: User
: This article mainly introduces the script for capturing website images in PHP. For more information about PHP tutorials, see. Method 1:


  Read_url ($ str); $ result = str_replace ("\" "," ", $ result); $ result = str_replace (" \ '"," ", $ result ); preg_match_all ('/|>)/I', $ result, $ matches); foreach ($ matches [1] as $ value) {echo $ value. "\ n"; $ this-> GrabImage ($ value, $ filename = "") ;}// $ url is the complete URL of the remote image and cannot be blank. // $ Filename is an optional variable. if it is null, the local file name is automatically generated based on the time and date. functionGrabImage ($ url, $ filename = "") {if ($ url = ""): returnfalse; endif; $ path = "download /"; // specify the storage folder // if the file does not exist, it is created; if (! File_exists ($ path) {mkdir ($ path);} if ($ filename = "") {$ ext = strrchr ($ url ,". "); if ($ ext! = ". Gif" & $ ext! = ". Jpg "): returnfalse; endif; @ $ filename = $ path. date ("YHis "). $ ext;} ob_start (); readfile ($ url); $ img = ob_get_contents (); ob_end_clean (); $ size = strlen ($ img ); $ fp2 = @ fopen ($ filename, "a"); fwrite ($ fp2, $ img); fclose ($ fp2); return $ filename ;}} $ download_img = new download_image (); $ download_img-> save_img (" http://www.jb51.net ");?>

Method 1 is relatively simple and clear, but there is a BUG, the picture is not completely captured, there is time to check again!

Method 2:


  Save_path = $ save_path; $ this-> img_size = $ img_size; if (! File_exists ($ save_path) {mkdir ($ save_path, 0775) ;}/ *** recursively downloads the method for capturing the homepage and its subpage images (recursive recursion) * @ param String $ capture_url the URL used to capture the image */publicfunctionrecursive_download_images ($ capture_url) {if (! In_array ($ capture_url, self: $ a_url_arr) {// no self: $ a_url_arr [] = $ capture_url captured; // included in static array} else {// crawled, directly exit the function return;} $ this-> download_current_page_images ($ capture_url ); // download all the images on the current page // use @ to block the warning error caused by the capture address being unreadable. $ content = @ file_get_contents ($ capture_url); // MatchA tagIn the href attribute? The previous regular expression $ a_pattern = "|] + href = ['\"]? ([^ '\ "?] +) ['\ ">] | U"; preg_match_all ($ a_pattern, $ content, $ a_out, PREG_SET_ORDER); $ tmp_arr = array (); // defines an array, used to store the currentLoopThe URL foreach ($ a_outas $ k => $ v) {/*** removes the null '','#', '/' and repeat value * 1: The value of the hyperlink address cannot be equal to the url of the currently crawled page. Otherwise, the url will die.Loop* 2: The hyperlink is ''or '#', and '/' is also the current page.Loop, * 3: sometimes a hyperlink address appears multiple times on a webpage. if it is not removed, a subpage is repeatedly downloaded) */if ($ v [1] &! In_array ($ v [1], self: $ a_url_arr )&&! In_array ($ v [1], array ('#', '/', $ capture_url) {$ tmp_arr [] = $ v [1];} foreach ($ tmp_arras $ k => $ v) {// hyperlink path address if (strpos ($ v, 'http ://')! = False) {// if the url contains http: //, you can directly access $ a_url = $ v;} else {// otherwise it turns out to be a relative address, URL $ domain_url = substr ($ capture_url, 0, strpos ($ capture_url, '/', 8) + 1); $ a_url = $ domain_url. $ v ;}$ this-> recursive_download_images ($ a_url );}} /*** download all images on the current webpage * @ param String $ capture_url: The webpage address used to capture images * @ return Array an Array of the URLs of all img tags on the current webpage * /publicfunctiondownload_current_page_images ($ capture_url) {$ content = @ file_get_c Ontents ($ capture_url); // shield the warning error // match the src attribute of the img tag? The previous regular expression $ img_pattern = "|] + src = ['\"]? ([^ '\ "?] +) ['\ ">] | U"; preg_match_all ($ img_pattern, $ content, $ img_out, PREG_SET_ORDER); $ photo_num = count ($ img_out ); // Number of matched images echo $ capture_url. "Found ". $ photo_num. "Picture \ n"; foreach ($ img_outas $ k = >$ v) {$ this-> save_one_img ($ capture_url, $ v [1]);} /*** method for saving a single image * @ param String $ capture_url the webpage address used to capture the image * @ param String $ img_url the url of the image to be saved */publicfunctionsave_one_img ($ capture_url, $ img_url) {// Image path address I F (strpos ($ img_url, 'http ://')! = False) {// $ img_url = $ img_url;} else {$ domain_url = substr ($ capture_url, 0, strpos ($ capture_url, '/', 8) + 1); $ img_url = $ domain_url. $ img_url;} $ pathinfo = pathinfo ($ img_url); // Obtain the image path information $ pic_name = $ pathinfo ['basename']; // Obtain the image name if (file_exists ($ this-> save_path. $ pic_name) {// If the image exists, it indicates that it has been captured. exit the echo $ img_url function. 'The image has been crawled! '. "\ N"; return;} // read the image content into a string $ img_data = @ file_get_contents ($ img_url ); // shield out the warning error because the image address cannot be read. if (strlen ($ img_data)> $ this-> img_size) {// download an image with a larger size than the limit $ img_size = file_put_contents ($ this-> save_path. $ pic_name, $ img_data); if ($ img_size) {echo $ img_url. 'The image is saved successfully! '. "\ N";} else {echo $ img_url.' an error occurred while saving the image! '. "\ N" ;}} else {echo $ img_url.' an error occurred while reading the image! '. "\ N" ;}}} set_time_limit (120); // set the maximum execution time of the script. set $ download_img = new download_image ('imgages/', 0) as needed ); // instantiate the downloaded imageObject// $ Download_img-> recursive_download_images ('http: // www.oschina.net /'); // recursive image capturing method // $ download_img-> download_current_page_images ($ _ POST ['capture _ url']); // only capture the current page image method $ download_img-> download_current_page_images ('http: // www.jb51.net '); // only capture the current page image method?>

Http://blog.csdn.net/china_skag/article/details/18452883
Http://www.jb51.net/article/21738.htm

Copyright disclaimer: reprinted. please keep the document signature and link

The above describes the script for capturing website images in PHP, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.