使用GD创建保持纵横比的缩略图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用GD创建保持纵横比的缩略图相关的知识,希望对你有一定的参考价值。

Will create a thumnail no taller or wider than the supplied size. Also contains some tips for reading and writing images using GD.
  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');

以上是关于使用GD创建保持纵横比的缩略图的主要内容,如果未能解决你的问题,请参考以下文章

使图像适应容器大小保持纵横比

PHP GD缩略图生成图像像素失真

Yii2 - Imagine - 创建缩略图并保持透明背景

保留图像中的纵横比::Magick 的缩略图功能

通过切割(不破坏纵横比)在Java中生成方形缩略图?

将 System.Drawing.Bitmap 缩放到给定大小,同时保持纵横比