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!