Create a thumbnail maintaining aspect ratio using GD


/ Published in: PHP
Save to your folder(s)

Will create a thumnail no taller or wider than the supplied size. Also contains some tips for reading and writing images using GD.


Copy this code and paste it in your HTML
  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.  
  10. $type = isset($info['type']) ? $info['type'] : $info[2];
  11.  
  12. // Check support of file type
  13. if ( !(imagetypes() & $type) )
  14. {
  15. // Server does not support file type
  16. return false;
  17. }
  18.  
  19. $width = isset($info['width']) ? $info['width'] : $info[0];
  20. $height = isset($info['height']) ? $info['height'] : $info[1];
  21.  
  22. // Calculate aspect ratio
  23. $wRatio = $maxSize / $width;
  24. $hRatio = $maxSize / $height;
  25.  
  26. // Using imagecreatefromstring will automatically detect the file type
  27. $sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
  28.  
  29. // Calculate a proportional width and height no larger than the max size.
  30. if ( ($width <= $maxSize) && ($height <= $maxSize) )
  31. {
  32. // Input is smaller than thumbnail, do nothing
  33. return $sourceImage;
  34. }
  35. elseif ( ($wRatio * $height) < $maxSize )
  36. {
  37. // Image is horizontal
  38. $tHeight = ceil($wRatio * $height);
  39. $tWidth = $maxSize;
  40. }
  41. else
  42. {
  43. // Image is vertical
  44. $tWidth = ceil($hRatio * $width);
  45. $tHeight = $maxSize;
  46. }
  47.  
  48. $thumb = imagecreatetruecolor($tWidth, $tHeight);
  49.  
  50. if ( $sourceImage === false )
  51. {
  52. // Could not load image
  53. return false;
  54. }
  55.  
  56. // Copy resampled makes a smooth thumbnail
  57. imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
  58. imagedestroy($sourceImage);
  59.  
  60. return $thumb;
  61. }
  62.  
  63. /**
  64.   * Save the image to a file. Type is determined from the extension.
  65.   * $quality is only used for jpegs.
  66.   * Author: mthorn.net
  67.   */
  68. function imageToFile($im, $fileName, $quality = 80)
  69. {
  70. if ( !$im || file_exists($fileName) )
  71. {
  72. return false;
  73. }
  74.  
  75. $ext = strtolower(substr($fileName, strrpos($fileName, '.')));
  76.  
  77. switch ( $ext )
  78. {
  79. case '.gif':
  80. imagegif($im, $fileName);
  81. break;
  82. case '.jpg':
  83. case '.jpeg':
  84. imagejpeg($im, $fileName, $quality);
  85. break;
  86. case '.png':
  87. imagepng($im, $fileName);
  88. break;
  89. case '.bmp':
  90. imagewbmp($im, $fileName);
  91. break;
  92. default:
  93. return false;
  94. }
  95.  
  96. return true;
  97. }
  98.  
  99. $im = thumbnail('temp.jpg', 100);
  100. imageToFile($im, 'temp-thumbnail.jpg');

URL: http://www.mthorn.net

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.