Home > Backend Development > PHP Tutorial > PHP implements a general method for downloading images

PHP implements a general method for downloading images

WBOY
Release: 2016-07-25 08:45:27
Original
1134 people have browsed it
  1. function getPicture($url,$pictureName){
  2. if ($url == "") return false;
  3. //Get the extension of the picture
  4. $info = getimagesize($url);
  5. $mime = $ info['mime'];
  6. $type = substr(strrchr($mime,'/'), 1);
  7. //Choose different image generation and saving functions for different image types
  8. switch($type){
  9. case 'jpeg':
  10. $img_create_func = 'imagecreatefromjpeg';
  11. $img_save_func = 'imagejpeg';
  12. $new_img_ext = 'jpg';
  13. break;
  14. case 'png':
  15. $img_create_func = 'imagecreatefrompng';
  16. $img_save_func = ' imagepng';
  17. $new_img_ext = 'png';
  18. break;
  19. case 'bmp':
  20. $img_create_func = 'imagecreatefrombmp';
  21. $img_save_func = 'imagebmp';
  22. $new_img_ext = 'bmp';
  23. break;
  24. case ' gif':
  25. $img_create_func = 'imagecreatefromgif';
  26. $img_save_func = 'imagegif';
  27. $new_img_ext = 'gif';
  28. break;
  29. case 'vnd.wap.wbmp':
  30. $img_create_func = 'imagecreatefromwbmp';
  31. $ img_save_func = 'imagewbmp';
  32. $new_img_ext = 'bmp';
  33. break;
  34. case 'xbm':
  35. $img_create_func = 'imagecreatefromxbm';
  36. $img_save_func = 'imagexbm';
  37. $new_img_ext = 'xbm';
  38. break;
  39. default:
  40. $img_create_func = 'imagecreatefromjpeg';
  41. $img_save_func = 'imagejpeg';
  42. $new_img_ext = 'jpg';
  43. }
  44. if ($pictureName == ""){
  45. $pictureName = time().". {$new_img_ext}";
  46. }else{
  47. $pictureName = $pictureName.".{$new_img_ext}";
  48. }
  49. $src_im = $img_create_func($url); //Create a new picture from url
  50. $img_save_func($ src_im, $pictureName); //Output file to file
  51. return $pictureName;
  52. }
Copy code

Download image, PHP


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