How to share PHP source code batch capture remote web page images and save them to a local device

Source: Internet
Author: User
Tags file url
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, please join us.

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, please join us.

As a website counterfeiter, when the website is copyrighted or even encrypted, WEBZIP is also turned off. How can I get pictures and background pictures on 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? 7.1.1 "; // 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 ,' http://www.jb51.net/ ') {$ Save_dir. =' http://www.jb51.net/ ';} // Create and save the 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.

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.