当前位置:Gxlcms > PHP教程 > phpgd库实现远程图片的下载

phpgd库实现远程图片的下载

时间:2021-07-01 10:21:17 帮助过:5人阅读

  1. /**
  2. * 远程图片的下载
  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. //获取图片的扩展名
  15. $info = getimagesize($url);
  16. $mime = $info['mime'];
  17. $type = substr(strrchr($mime,'/'), 1);
  18. //不同的图片类型选择不同的图片生成和保存函数
  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); //由url创建新图片
  61. $img_save_func($src_im, $pictureName); //输出文件到文件
  62. return $pictureName;
  63. }
  64. ?>

2,网页内容部分

运行结果如下图:(图片自动保存在当前文件目录) php gd库实现远程图片下载

人气教程排行