Home >Backend Development >PHP Tutorial >PHP gd library implements remote image downloading

PHP gd library implements remote image downloading

WBOY
WBOYOriginal
2016-07-25 08:55:12768browse
  1. /**
  2. * Remote picture 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 '
    &lt ;img src="'.$img.'">
    ';
  11. }
  12. function getPicture($url,$pictureName){
  13. if ($url == "") return false;
  14. // Get the extension of the image
  15. $info = getimagesize($url);
  16. $mime = $info['mime'];
  17. $type = substr(strrchr($mime,'/'), 1);
  18. //different Choose different image generation and saving functions for the image type
  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 +
  61. 2. Web content part
Remote url address:
File name:

    Copy the code
  1. The running result is as follows: (The picture is automatically saved in the current file directory)

php gd库实现远程图片下载

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn