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

phpgd实现下载图片

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

代码转载于:
  1. header("Content-type:text/html ; charset=utf-8");
  2. if (!empty($_POST['submit'])){
  3. $url = $_POST['url'];
  4. $pictureName = $_POST['pictureName'];
  5. $img = getPicture($url,$pictureName);
  6. echo '
    ';
  7. }
  8. function getPicture($url,$pictureName){
  9. if ($url == "") return false;
  10. //获取图片的扩展名
  11. $info = getimagesize($url);
  12. $mime = $info['mime'];
  13. $type = substr(strrchr($mime,'/'), 1);
  14. //不同的图片类型选择不同的图片生成和保存函数
  15. switch($type){
  16. case 'jpeg':
  17. $img_create_func = 'imagecreatefromjpeg';
  18. $img_save_func = 'imagejpeg';
  19. $new_img_ext = 'jpg';
  20. break;
  21. case 'png':
  22. $img_create_func = 'imagecreatefrompng';
  23. $img_save_func = 'imagepng';
  24. $new_img_ext = 'png';
  25. break;
  26. case 'bmp':
  27. $img_create_func = 'imagecreatefrombmp';
  28. $img_save_func = 'imagebmp';
  29. $new_img_ext = 'bmp';
  30. break;
  31. case 'gif':
  32. $img_create_func = 'imagecreatefromgif';
  33. $img_save_func = 'imagegif';
  34. $new_img_ext = 'gif';
  35. break;
  36. case 'vnd.wap.wbmp':
  37. $img_create_func = 'imagecreatefromwbmp';
  38. $img_save_func = 'imagewbmp';
  39. $new_img_ext = 'bmp';
  40. break;
  41. case 'xbm':
  42. $img_create_func = 'imagecreatefromxbm';
  43. $img_save_func = 'imagexbm';
  44. $new_img_ext = 'xbm';
  45. break;
  46. default:
  47. $img_create_func = 'imagecreatefromjpeg';
  48. $img_save_func = 'imagejpeg';
  49. $new_img_ext = 'jpg';
  50. }
  51. if ($pictureName == ""){
  52. $pictureName = time().".{$new_img_ext}";
  53. }else{
  54. $pictureName = $pictureName.".{$new_img_ext}";
  55. }
  56. $src_im = $img_create_func($url); //由url创建新图片
  57. $img_save_func($src_im, $pictureName); //输出文件到文件
  58. return $pictureName;
  59. }
  60. ?>

人气教程排行