PHP using the GD library to download all the pictures in the Web page, _php tutorial

Source: Internet
Author: User
Tags imagejpeg

PHP in the use of the GD library to download all the pictures on the page,


In the early PHP tutorial on the PHP GD Library can achieve remote image download, but that just download a picture, the principle is the same, to download a Web page all the pictures as long as the use of regular expressions to judge, find all the image URL can be recycled to download, I specifically reference the network resources to write the GD library image download Class!
The PHP code is as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "), if (!empty ($_post[' submit ')) {$url = $_post[' url '), or//To get a picture of a relative path $url _fields = Parse_url ($url); $main _url = $url _fields[' host '); $base _url = substr ($url, 0,strrpos ($url, '/') +1); Get Web page content//set proxy Server $opts = Array (' http ' =>array (' Request_fulluri ' =>true)); $context = Stream_context_create ($opts); $content = File_get_contents ($url, False, $context); Match the IMG tag, save all matching strings to the array $matches $reg = "//i"; Preg_match_all ($reg, $content, $matches); $count = count ($matches [0]); for ($i =0; $i < $count; $i + +) {/* Converts the URL of all pictures to lowercase * $matches [1][$i] = Strtolower ($matches [1][$i]); *////If the picture is a relative path, convert to full path I F (!strpos (' a '. $matches [1][$i], ' http ')} {//Because '/' is the No. 0 position if (strpos (' a '. $matches [1][$i], '/') {$matches [1][$i] = ' http:  '. $main _url. $matches [1][$i];  }else{$matches [1][$i] = $base _url. $matches [1][$i]; }}}//filter duplicate images $img _arr = Array_unique ($matches [1]); Instantiate picture Download class $getImg = new Downimage (); $url _count = count ($img _arr); for ($i =0; $i <$Url_count; $i + +) {$getImg->source = $img _arr[$i]; $getImg->save_address = './pic/'; $file = $getImg->download ();} echo "Finished downloading Yes! Haha, simple! ";} Class downimage{public $source;//Remote picture URL public $save _address;//save local address public $set _extension;//Set Picture extension public $quality ; Picture quality (0~100,100 best, default 75 or so)//download method (choose GD Library image download) public function download () {//Get remote picture information $info = @getimagesize ($this SOURCE); Get picture Extension $mime = $info [' MIME ']; $type = substr (STRRCHR ($mime, '/'), 1);  Different picture types choose different picture generation and save function switch ($type) {case ' jpeg ': $img _create_func = ' imagecreatefromjpeg ';  $img _save_func = ' imagejpeg ';  $new _img_ext = ' jpg '; $image _quality = isset ($this->quality)?  $this->quality:100;  Break  Case ' png ': $img _create_func = ' imagecreatefrompng ';  $img _save_func = ' imagepng ';  $new _img_ext = ' png ';  Break  Case ' BMP ': $img _create_func = ' imagecreatefrombmp ';  $img _save_func = ' imagebmp ';  $new _img_ext = ' bmp ';  Break  Case ' gif ': $img _create_func = ' imagecreatefromgif '; $img _save_func = 'Imagegif ';  $new _img_ext = ' gif ';  Break  Case ' vnd.wap.wbmp ': $img _create_func = ' imagecreatefromwbmp ';  $img _save_func = ' imagewbmp ';  $new _img_ext = ' bmp ';  Break  Case ' XBM ': $img _create_func = ' IMAGECREATEFROMXBM ';  $img _save_func = ' IMAGEXBM ';  $new _img_ext = ' XBM ';  Break  Default: $img _create_func = ' imagecreatefromjpeg ';  $img _save_func = ' imagejpeg '; $new _img_ext = ' jpg ';  }//Based on whether the extension is set to synthesize the local file name if (isset ($this->set_extension)) {$ext = STRRCHR ($this->source, ".");  $strlen = strlen ($ext); $newname = basename (substr ($this->source,0,-$strlen)). $new _img_ext;  }else{$newname = basename ($this->source);} Generate local file path $save _address = $this->save_address. $newname; $img = @ $img _create_func ($this->source); if (Isset ($image _quality)) {$save _img = @ $img _save_func ($img, $save _address, $image _quality);}  else{$save _img = @ $img _save_func ($img, $save _address);} return $save _img; }}?>

Run results

http://www.bkjia.com/PHPjc/998564.html www.bkjia.com true http://www.bkjia.com/PHPjc/998564.html techarticle PHP in the use of the GD library to download all the pictures in the Web page, in the early PHP tutorial on the PHP GD Library can achieve remote image download, but that just download a picture, the principle is the same ...

  • 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.