Home > Backend Development > PHP Tutorial > php gd implements downloading pictures

php gd implements downloading pictures

WBOY
Release: 2016-07-25 08:51:12
Original
866 people have browsed it
Code reproduced from:
  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 '
    <img src="'.$img. '">
    ';
  7. }
  8. function getPicture($url,$pictureName){
  9. if ($url == "") return false;
  10. //Get the extension of the picture
  11. $info = getimagesize ($url);
  12. $mime = $info['mime'];
  13. $type = substr(strrchr($mime,'/'), 1);
  14. //Choose different image generation and saving for different image types Function
  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); // Create a new picture from url
  57. $img_save_func($src_im, $pictureName); //Output file to file
  58. return $pictureName;
  59. }
  60. ?>
  61. Remote url address:
  62. File name:
Copy code


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template