This article will share with you how to batch capture remote Web page images from the PHP source code and save them to a local machine. if you are interested in the batch capture of web page images, join us as a counterfeit station worker, when a website is copyrighted or even encrypted, WEBZIP is also turned off. how can I deduct images and background images from the webpage. Sometimes, you may think of using Firefox. This browser seems to be a powerful BUG. the article is copyrighted and the right-click is blocked. Firefox will not be affected at all.
But as a developer who loves php, they prefer to do it on their own. Therefore, I wrote the following source code, which allows php to remotely capture image applets. You can read the css file and capture the background image in the css code. the following code is also written to capture the image in the css.
<? Php header ("Content-Type: text/html; charset = utf-8"); error_reporting (E_ERROR | E_WARNING); // global configuration $ fromFile = "aaa.css? 1.1.9 "; // the file to be crawled $ savePath =" ttttttttt "; // save path $ fromDomain =" http://www.xxx.com/ "; // The domain name to be crawled // read the css style and extract the URLs of all images $ str = file_get_contents ($ fromFile); $ strArr = explode (" url (", $ str); $ I = 0; foreach ($ strArr as $ val) {$ val1 = explode (")", $ val ); if (strpos ($ val1 [0], 'jpg ') | strpos ($ val1 [0], 'PNG') | strpos ($ val1 [0], 'gif') $ imgUrl [$ I ++] = $ val1 [0];} // PS: you can use a regular expression above, but I think this is also good. // start to capture foreach ($ imgUrl as $ url) {if ($ url = "") continue; $ filename = $ savePath. $ url; $ url = $ fromDomain. $ url; getImage ($ url, $ filename);} function getImage ($ url, $ filename) {ob_start (); $ context = stream_context_create (array ('http '=> array ('follow _ location' => false // don't follow redirects); // Make sure php. fopen wrappers in ini has activated readfile ($ url, false, $ context); $ img = ob_get_contents (); ob_end_clean (); $ fp2 = @ fopen ($ filename, "a"); fwrite ($ fp2, $ img); fclose ($ fp2); echo $ filename. "OK √
";}?>
If there is no accident, you will find that all the images under the specified folder are full, haha ..
Ps: php retrieves remote images and downloads and saves them to a local device.
Share a function code that uses php to obtain remote images and download and save them to a local device:
/** Function: php perfectly implements remote image download and saving to local * parameter: file url, save file directory, save file name, download method * when the file name is saved as null, use the original name of the remote file */function getImage ($ url, $ save_dir = '', $ filename = '', $ type = 0) {if (trim ($ url) = '') {return array ('File _ name' => '', 'SAVE _ path' => '', 'error' => 1);} if (trim ($ save_dir) ='') {$ save_dir = '. /';} if (trim ($ filename) = '') {// Save the file name $ ext = strrchr ($ url ,'. '); if ($ ext! Using '.gif '& $ ext! Else '.jpg ') {return array ('File _ name' => '', 'SAVE _ path' =>'', 'error' => 3 );} $ filename = time (). $ ext;} if (0! = Strrpos ($ save_dir, '/') {$ save_dir. = '/';} // Create a save directory if (! File_exists ($ save_dir )&&! Mkdir ($ save_dir, 0777, true) {return array ('File _ name' => '', 'SAVE _ path' => '', 'Error' => 5);} // method used to obtain remote files if ($ type) {$ ch = curl_init (); $ timeout = 5; curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout ); $ img = curl_exec ($ ch); curl_close ($ ch);} else {ob_start (); readfile ($ url); $ img = ob_get_contents (); ob_end_clean ();} // $ size = strlen ($ img); // File size $ fp2 = @ fopen ($ save_dir. $ filename, 'A'); fwrite ($ fp2, $ img); fclose ($ fp2); unset ($ img, $ url ); return array ('File _ name' => $ filename, 'SAVE _ path' => $ save_dir. $ filename, 'error' => 0 );}
The above content is a small part of the PHP source code to share with you batch capture remote Web page images and save them to a local implementation method, I hope you like it.