Phpgd library for remote image download

Source: Internet
Author: User
Tags imagejpeg
Phpgd library for remote image download

  1. /**
  2. * Remote Image Download
  3. * By bbs.it-home.org
  4. */
  5. Header ("Content-type: text/html; charset = utf-8 ");
  6. If (! Empty ($ _ POST ['submit ']) {
  7. $ Url = $ _ POST ['URL'];
  8. $ PictureName = $ _ POST ['picturename'];
  9. $ Img = getPicture ($ url, $ pictureName );
  10. Echo'
    ';
  11. }
  12. Function getPicture ($ url, $ pictureName ){
  13. If ($ url = "") return false;
  14. // Obtain the image extension
  15. $ Info = getimagesize ($ url );
  16. $ Mime = $ info ['Mime '];
  17. $ Type = substr (strrchr ($ mime, '/'), 1 );
  18. // Select different image generation and storage functions for different Image types
  19. Switch ($ type ){
  20. Case 'jpeg ':
  21. $ Img_create_func = 'imagecreatefromjpeg ';
  22. $ Img_save_func = 'imagejpeg ';
  23. $ New_img_ext = 'jpg ';
  24. Break;
  25. Case 'PNG ':
  26. $ Img_create_func = 'imagecreatefrompng ';
  27. $ Img_save_func = 'imagepng ';
  28. $ New_img_ext = 'PNG ';
  29. Break;
  30. Case 'bmp ':
  31. $ Img_create_func = 'imagecreatefrombmp ';
  32. $ Img_save_func = 'imagebmp ';
  33. $ New_img_ext = 'bmp ';
  34. Break;
  35. Case 'GIF ':
  36. $ Img_create_func = 'imagecreatefromgif ';
  37. $ Img_save_func = 'imagegif ';
  38. $ New_img_ext = 'GIF ';
  39. Break;
  40. Case 'vnd. wap. wbmp ':
  41. $ Img_create_func = 'imagecreatefromwbmp ';
  42. $ Img_save_func = 'imagewbmp ';
  43. $ New_img_ext = 'bmp ';
  44. Break;
  45. Case 'xbm ':
  46. $ Img_create_func = 'imagecreatefromxbm ';
  47. $ Img_save_func = 'imagexbm ';
  48. $ New_img_ext = 'xbm ';
  49. Break;
  50. Default:
  51. $ Img_create_func = 'imagecreatefromjpeg ';
  52. $ Img_save_func = 'imagejpeg ';
  53. $ New_img_ext = 'jpg ';
  54. }
  55. If ($ pictureName = ""){
  56. $ PictureName = time (). ". {$ new_img_ext }";
  57. } Else {
  58. $ PictureName = $ pictureName. ". {$ new_img_ext }";
  59. }
  60. $ Src_im = $ img_create_func ($ url); // create a new image from the url
  61. $ Img_save_func ($ src_im, $ pictureName); // output the file to the file
  62. Return $ pictureName;
  63. }
  64. ?>

2. webpage content

The running result is as follows: (The image is automatically saved in the current file directory)

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.