Use the gd Library in php to download all images on the webpage ,. Php uses the gd library to download all the images on the webpage. in the previous php Tutorial, we talked about the phpgd library to download remote images, but it only downloads an image, the principle is that in php, the gd library is used to download all images on the webpage,
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:
<? Phpheader ("Content-type: text/html; charset = utf-8"); if (! Empty ($ _ POST ['submit ']) {$ url = $ _ POST ['URL']; // the operation to get the image of the relative path $ url_fields = parse_url ($ url); $ main_url = $ url_fields ['host']; $ base_url = substr ($ url, 0, strrpos ($ url, '/') + 1 ); // get webpage 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_matc H_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_ad Dress = './pic/'; $ file = $ getImg-> download ();} echo "download complete! Haha, that's easy! ";} Class DownImage {public $ source; // remote image URL public $ 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 = 'Authorization'; $ 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 = s Trlen ($ ext); $ newname = basename (substr ($ 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
Ghost, in the early php Tutorial, we talked about how the php gd library can be used to download remote images, but it just downloads an image. The principle is the same...