PHP simple web page image capturing

Source: Internet
Author: User
Example? PhpincludeimgCatcher. class. php; header (content-type: texthtml; charsetutf-8); $ catcherNewimgCatcher (www.oschina.net, images, jpg); $ catcher-get_pic ();? None? PhpclassimgCatcher {constFLODER_DIRimage; constIMG_TY

Example? Php include imgCatcher. class. php; header (content-type: text/html; charset = UTF-8); $ catcher = New imgCatcher (http://www.oschina.net/,images,jpg); $ catcher-get_pic ();? None? Php class imgCatcher {const FLODER_DIR = 'image/'; const IMG_TY

Example

Include "imgCatcher. class. php ";
Header ('content-type: text/html; charset = UTF-8 ');
$ Catcher = New imgCatcher ("http://www.oschina.net/", "images", "jpg ");

$ Catcher-> get_pic ();

?> <无>
  Url = $ url; $ url_arr = parse_url ($ url); $ this-> domain = array_shift ($ url_arr); if (trim ($ floder) = "") {$ this-> folder = FLODER_DIR;} else {$ this-> folder = $ floder. '/';} if (trim ($ type) = "") {$ this-> type = IMG_TYPE;} else {$ this-> type = $ type ;} $ this-> pic_arr = array (); if (! Is_dir (_ DIR __. '/'. $ this-> folder) mkdir (_ DIR __. '/'. $ this-> folder);} public function get_pic () {set_time_limit (0); // The capture is not subject to the time limit. // get the image binary stream $ data = self :: curlGet ($ this-> url); // use a regular expression to obtain the image link $ pattern_src = '/type. ')). *?> /'; // $ Num = preg_match_all ($ pattern_src, $ data, $ match_src); $ this-> pic_arr = $ match_src [1]; // obtain the Image array $ this-> get_name (); return 0;} public function get_name () {$ pic_arr = $ this-> pic_arr; // Image number and type $ pattern_type = '/. *\/(. *?) $/'; Foreach ($ pic_arr as $ pic_item) {// cyclically retrieve the address of each graph $ num = preg_match_all ($ pattern_type, $ pic_item, $ match_type ); // Save the image as a stream $ write_fd = fopen ($ this-> folder. $ match_type [1] [0], "wb"); fwrite ($ write_fd, self: CurlGet ($ pic_item, $ this-> url )); fclose ($ write_fd);} return 0;} // capture the webpage content static function CurlGet ($ url, $ domain = "") {if (substr ($ url, 0, 1) = "/") {$ url = $ domain. $ url;} if (substr ($ url, 0, 1) = ". ") {$ Url = $ domain. substr ($ url, 1);} if (substr ($ url, 0, 2) = ".. ") {$ url = $ domain. substr ($ url, 2) ;}$ url = str_replace ('&', '&', $ url); $ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_HEADER, false); // curl_setopt ($ curl, CURLOPT_REFERER, $ url); curl_setopt ($ curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2) "); Curl_setopt ($ curl, scheme, 'cookie.txt '); curl_setopt ($ curl, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt ($ curl, scheme, 1); curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 0); $ values = curl_exec ($ curl); curl_close ($ curl); return $ values ;}}?>

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.