Gd Library image download class to download all images on the webpage

Source: Internet
Author: User
Tags imagejpeg
In the previous php Tutorial, the phpgd library can be used to download remote images. However, it only downloads an image in the same principle, to download all the images on a Web page, you only need to use regular expressions to judge and find all the image URLs and download them cyclically.

In the previous php Tutorial, the php gd library can be used to download remote images. However, it only downloads an image in the same principle, to download all images on a Web page, you only need to use regular expressions to judge and find out all the image URLs to download them cyclically. I specially wrote the gd Library image download class by referring to network resources!

The php code is as follows:


 Array ('request _ fulluri '=> true); $ context = stream_context_create ($ opts); $ content = file_get_contents ($ url, false, $ context ); // match the img tag and 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 ++) {/* convert the URLs of all images to lowercase letters * $ matches [1] [$ I] = strtolower ($ matches [1] [$ I]); * /// if the image is relative, it is converted to full path if (! Strpos ('A '. $ matches [1] [$ I], 'http ') {// because'/'is 0th locations 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 the image 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 "download complete! Haha, that's easy! ";} Class DownImage {public $ source; // remote image URLpublic $ save_address; // save the local address public $ set_extension; // set the image Extension public $ quality; // image quality (0 ~ 100,100 best, about 75 by default) // download method (using GD Library image download) public function download () {// obtain remote image information $ info = @ getimagesize ($ this-> source); // Obtain the image extension $ mime = $ info ['Mime ']; $ type = substr (strrchr ($ mime, '/'), 1); // select different Image types, and select the 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 = 'alipay '; $ img_save_func = 'imagegif'; $ new_img_ext = 'GIF'; break; case 'vnd. wap. wbmp ': $ img_create_func = 'imagecreatefrom Wbmp '; $ img_save_func = 'imagewbmp'; $ new_img_ext = 'bmp '; break; case 'xbm': $ img_create_func = 'authorization'; $ img_save_func = 'imagexbm '; $ new_img_ext = 'xbm '; break; default: $ img_create_func = 'imagecreatefromjpeg'; $ img_save_func = 'imagejpeg '; $ new_img_ext = 'jpg ';} // combine the local file name if (isset ($ this-> set_extension) {$ ext = strrchr ($ this-> source ,". "); $ strlen = strlen ($ ext); $ newname = basename (sub Str ($ this-> source, 0,-$ strlen )). '. '. $ new_img_ext;} else {$ newname = basename ($ this-> source);} // Generate the 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 ;}}?>

Running result

The downloaded image is saved in the pic folder of the current directory in this example!

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.