Simple PHP thumbnails generation function

Source: Internet
Author: User
Simple PHP thumbnails generation function
A simple thumbnail generation function supports image formats such as gif, jpeg, png, and bmp. it supports scaling to a fixed size or proportional scaling. it supports directly inputting the image to a browser or saving the image to a file.

  1. /**
  2. * Simple thumbnails generation function (supported Image formats: gif, jpeg, png, and bmp)
  3. * @ Authorxiaoshuoit@163.com
  4. * @ Paramstring $ src source image path
  5. * @ Paramint $ width the width of the thumbnail (proportional scaling is performed only when the height is specified)
  6. * @ Paramint $ width the height of the thumbnail (proportional scaling is performed only when the width is specified)
  7. * @ Paramstring $ filename storage path (output directly to the browser when 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, $ 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", 100, null, 'IMG/example_thumb.jpg ');

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.