This article mainly introduces how to use the gd Library in php to implement remote image download instances. This article provides the implementation code directly. if you need it, you can refer to it because you want to write a remote image download class today, warm-up in advance wrote a php gd library to achieve remote image download, of course, curl is better, php gd library to achieve remote image download function mainly uses the gd Library's two functions ImageCreateFromXXX () this function is used to generate image functions and ImageXXX functions. XXX indicates the extension of different images. Therefore, you have to find a way to obtain the extension of remote images. 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,'/'), 1 ); // for different Image types, select 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 = 'im Agepng '; $ new_img_ext = 'PNG'; break; case 'bmp ': $ img_create_func = 'alipay'; $ 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_c Reate_func = 'authorization'; $ img_save_func = 'imagexbm '; $ new_img_ext = 'xbm'; break; default: $ img_create_func = 'authorization'; $ img_save_func = 'imagejpeg '; $ 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 $ img_save_func ($ src_im, $ pictureNa Me); // return $ pictureName to the output file;}?>
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)