Simple php-generated thumbnail function

Source: Internet
Author: User
Simple generation of thumbnail functions, support image format: GIF, JPEG, PNG and BMP, support zoom to fixed size or zoom, support direct input to browser or save to file.
  1. /**
  2. * Simple generation of thumbnail functions (supported image formats: GIF, JPEG, PNG, and BMP)
  3. * @authorxiaoshuoit @163.com
  4. * @paramstring $src Source picture path
  5. * @paramint $width thumbnail width (equal to zoom only when height is specified)
  6. * @paramint $width thumbnail height (equal to zoom only when width is specified)
  7. * @paramstring $filename save path (direct output to browser if not specified)
  8. * @returnbool
  9. */
  10. function Simple_thumb ($src, $width = null, $height = NULL, $filename = null) {
  11. if (!isset ($width) &&!isset ($height))
  12. return false;
  13. if (Isset ($width) && $width <= 0)
  14. return false;
  15. if (Isset ($height) && $height <= 0)
  16. return false;
  17. $size = getimagesize ($SRC);
  18. if (! $size)
  19. return false;
  20. List ($src _w, $src _h, $src _type) = $size;
  21. $SRC _mime = $size [' MIME '];
  22. Switch ($src _type) {
  23. Case 1:
  24. $img _type = ' gif ';
  25. Break
  26. Case 2:
  27. $img _type = ' jpeg ';
  28. Break
  29. Case 3:
  30. $img _type = ' png ';
  31. Break
  32. Case 15:
  33. $img _type = ' wbmp ';
  34. Break
  35. Default:
  36. return false;
  37. }
  38. if (!isset ($width))
  39. $width = $src _w * ($height/$src _h);
  40. if (!isset ($height))
  41. $height = $src _h * ($width/$src _w);
  42. $imagecreatefunc = ' Imagecreatefrom '. $img _type;
  43. $src _img = $imagecreatefunc ($SRC);
  44. $dest _img = Imagecreatetruecolor ($width, $height);
  45. Imagecopyresampled ($dest _img, $src _img, 0, 0, 0, 0, $width, $height, $src _w, $src _h);
  46. $imagefunc = ' image '. $img _type;
  47. if ($filename) {
  48. $imagefunc ($dest _img, $filename);
  49. } else {
  50. Header (' Content-type: '. $src _mime);
  51. $imagefunc ($dest _img);
  52. }
  53. Imagedestroy ($src _img);
  54. Imagedestroy ($dest _img);
  55. return true;
  56. }
  57. Simple_thumb ("Http://www.baidu.com/img/bdlogo.gif", 100);
  58. Simple_thumb ("img/example.jpg", +, NULL, ' img/example_thumb.jpg ');
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.