This article describes how to use the gd library in php to implement remote image download instances. This article provides the implementation code directly. For more information, see
This article describes how to use the gd library in php to implement remote image download instances. This article provides the implementation code directly. For more information, see
Because today I want to write a remote image download class, I have prepared a php gd library in advance to enable remote image download. Of course, curl is better ,, the remote image download function of the php gd library mainly uses the two functions ImageCreateFromXXX () of the gd library to generate image functions and ImageXXX functions. XXX indicates the extensions of different images, so you have to find a way to get the extension of the remote image. The php code is attached as follows:
<? Phpheader ("Content-type: text/html; charset = UTF-8"); if (! Empty ($ _ POST ['submit ']) {$ url = $ _ POST ['url']; $ pictureName = $ _ POST ['picturename']; $ img = getPicture ($ url, $ pictureName); echo'
';} Function getPicture ($ url, $ pictureName) {if ($ url = "") return false; // get the image extension $ info = getimagesize ($ url ); $ mime = $ info ['mime ']; $ type = substr (strrchr ($ mime ,' http://www.jb51.net/ '), 1); // select a different image type. The function switch ($ type) {case 'jpeg': $ img_create_func = 'imagecreatefromjpeg '; $ img_save_func = 'imagejpeg '; $ new_img_ext = 'jpg'; 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_c Reate_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 = 'category '; $ img_save_func = 'imagexbm '; $ new_img_ext = 'xbm'; break; default: $ img_create_func = 'imagecreatefromjpeg '; $ img_save_func = 'image Jpeg '; $ new_img_ext = 'jpg';} if ($ pictureName = "") {$ pictureName = time (). ". {$ new_img_ext} ";} else {$ pictureName = $ pictureName. ". {$ new_img_ext} ";}$ src_im = $ img_create_func ($ url); // create a new image from the url $ img_save_func ($ src_im, $ pictureName ); // output the file to the file return $ pictureName;}?>
The running result is as follows: (the image is automatically saved in the current file directory. If you do not understand it, leave a message)