A function used to create image thumbnails.

Source: Internet
Author: User
A function used to create image thumbnails.

  1. /**

  2. * Create an image thumbnail and return the true result.
  3. *
  4. * @ Param string $ cat Directory
  5. * @ Param string $ oldname original image file name
  6. * @ Param string $ newname New graph file name
  7. * @ Param int $ width: thumbnail width
  8. * @ Param int $ height the Thumbnail height
  9. * @ Return
  10. */
  11. Function thumb ($ cat, $ oldname, $ newname, $ width = 160, $ height = 120 ){
  12. $ SrcFile = $ cat. "/". $ oldname;
  13. $ Data = getimagesize ($ srcFile );
  14. $ DscFile = $ cat. "/". $ newname;

  15. Switch ($ data [2]) {

  16. Case 1:
  17. $ Im = imagecreatefromgif ($ srcFile );
  18. Break;

  19. Case 2:

  20. $ Im = imagecreatefromjpeg ($ srcFile );
  21. Break;

  22. Case 3:

  23. $ Im = imagecreatefrompng ($ srcFile );
  24. Break;
  25. }

  26. $ SrcW = imagesx ($ im );

  27. $ SrcH = imagesy ($ im );

  28. If ($ srcW/$ width) >=( $ srcH/$ height )){

  29. $ Temp_height = $ height;
  30. $ Temp_width = $ srcW/($ srcH/$ height );
  31. $ Src_X = abs ($ width-$ temp_width)/2 );
  32. $ Src_Y = 0;
  33. }
  34. Else {
  35. $ Temp_width = $ width;
  36. $ Temp_height = $ srcH/($ srcW/$ width );
  37. $ Src_X = 0;
  38. $ Src_Y = abs ($ height-$ temp_height)/2 );
  39. }

  40. $ Temp_img = imagecreatetruecolor ($ temp_width, $ temp_height );

  41. Imagecopyresized ($ temp_img, $ im, 0, 0, 0, $ temp_width, $ temp_height, $ srcW, $ srcH );

  42. $ Ni = imagecreatetruecolor ($ width, $ height );

  43. Imagecopyresized ($ ni, $ temp_img, 0, 0, $ src_X, $ src_Y, $ width, $ height, $ width, $ height );
  44. $ Cr = imagejpeg ($ ni, $ dscFile );

  45. If ($ cr ){

  46. Chmod ($ dscFile, 0777 );
  47. Return true;
  48. }
  49. }
  50. ?>

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.