PHP uses GD to create thumbnails that maintain aspect ratios

Source: Internet
Author: User
  1. /**
  2. * Create a thumbnail image from $inputFileName no taller or wider than
  3. * $maxSize. Returns the new image resource or false on error.
  4. * Author:mthorn.net
  5. */
  6. function thumbnail ($inputFileName, $maxSize = 100)
  7. {
  8. $info = getimagesize ($inputFileName);
  9. $type = Isset ($info [' type '])? $info [' type ']: $info [2];
  10. Check support of file type
  11. if (! ( Imagetypes () & $type))
  12. {
  13. Server does not support file type
  14. return false;
  15. }
  16. $width = Isset ($info [' width '])? $info [' width ']: $info [0];
  17. $height = Isset ($info [' height '])? $info [' Height ']: $info [1];
  18. Calculate aspect ratio
  19. $wRatio = $maxSize/$width;
  20. $hRatio = $maxSize/$height;
  21. Using imagecreatefromstring would automatically detect the file type
  22. $sourceImage = imagecreatefromstring (file_get_contents ($inputFileName));
  23. Calculate a proportional width and height no larger than the max size.
  24. if ($width <= $maxSize) && ($height <= $maxSize))
  25. {
  26. Input is smaller than thumbnail
  27. return $sourceImage;
  28. }
  29. ElseIf (($wRatio * $height) < $maxSize)
  30. {
  31. Image is Horizontal
  32. $tHeight = Ceil ($wRatio * $height);
  33. $tWidth = $maxSize;
  34. }
  35. Else
  36. {
  37. Image is vertical
  38. $tWidth = Ceil ($hRatio * $width);
  39. $tHeight = $maxSize;
  40. }
  41. $thumb = Imagecreatetruecolor ($tWidth, $tHeight);
  42. if ($sourceImage = = = False)
  43. {
  44. Could Not load image
  45. return false;
  46. }
  47. Copy resampled makes a smooth thumbnail
  48. Imagecopyresampled ($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
  49. Imagedestroy ($sourceImage);
  50. return $thumb;
  51. }
  52. /**
  53. * Save the image to a file. Type is determined from the extension.
  54. * $quality is only used for JPEGs.
  55. * Author:mthorn.net
  56. */
  57. function Imagetofile ($im, $fileName, $quality = 80)
  58. {
  59. if (! $im | | file_exists ($FILENAME))
  60. {
  61. return false;
  62. }
  63. $ext = Strtolower (substr ($fileName, Strrpos ($fileName, '. ')));
  64. Switch ($ext)
  65. {
  66. Case '. gif ':
  67. Imagegif ($im, $fileName);
  68. Break
  69. Case '. jpg ':
  70. Case '. JPEG ':
  71. Imagejpeg ($im, $fileName, $quality);
  72. Break
  73. Case '. png ':
  74. Imagepng ($im, $fileName);
  75. Break
  76. Case '. bmp ':
  77. Imagewbmp ($im, $fileName);
  78. Break
  79. Default
  80. return false;
  81. }
  82. return true;
  83. }
  84. $im = Thumbnail (' temp.jpg ', 100);
  85. Imagetofile ($im, ' temp-thumbnail.jpg ');
Copy Code
Php
  • 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.