The gd library is used in php to implement remote image download instances,

Source: Internet
Author: User
Tags imagejpeg

The gd library is used in php to implement remote image download instances,

Because today I want to write a remote image download class, I have prepared a php gd library in advance to implement the remote image download function. 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 '<pre>  </pre>';} function getPicture ($ url, $ pictureName) {if ($ url =" ") return false; // get the image extension $ info = getimagesize ($ url); $ mime = $ info ['mime ']; $ type = substr (strrchr ($ mime, '/'), 1); // function for generating and saving different image types: switch ($ type) {case 'jpeg ': $ img_create_f Unc = '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_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_create_func = 'category '; $ img_save_func = 'imagexbm '; $ new_img_ext = 'xbm'; break; default: $ img_create_func = 'hangzhou'; $ img_save_func = 'imagejpeg '; $ new_img_ext = 'jpg ';} if ($ pictureName = "") {$ pictureName = t Ime (). ". {$ 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;}?> <Form method = "POST" action = ""> remote url: <input type = "text" name = "url" size = 20/> <br/> File name: <input type = "text" name = "pictureName" size = 20/> <input type = "submit" name = "submit" value = "Download"/> </form>

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)


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.