php使用GD创建保持宽高比的缩略图

WBOY
Lepaskan: 2016-07-25 08:43:07
asal
931 orang telah melayarinya
  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 will 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 {
  25. // Input is smaller than thumbnail, do nothing
  26. return $sourceImage;
  27. }
  28. elseif ( ($wRatio * $height) {
  29. // Image is horizontal
  30. $tHeight = ceil($wRatio * $height);
  31. $tWidth = $maxSize;
  32. }
  33. else
  34. {
  35. // Image is vertical
  36. $tWidth = ceil($hRatio * $width);
  37. $tHeight = $maxSize;
  38. }
  39. $thumb = imagecreatetruecolor($tWidth, $tHeight);
  40. if ( $sourceImage === false )
  41. {
  42. // Could not load image
  43. return false;
  44. }
  45. // Copy resampled makes a smooth thumbnail
  46. imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
  47. imagedestroy($sourceImage);
  48. return $thumb;
  49. }
  50. /**
  51. * Save the image to a file. Type is determined from the extension.
  52. * $quality is only used for jpegs.
  53. * Author: mthorn.net
  54. */
  55. function imageToFile($im, $fileName, $quality = 80)
  56. {
  57. if ( !$im || file_exists($fileName) )
  58. {
  59. return false;
  60. }
  61. $ext = strtolower(substr($fileName, strrpos($fileName, '.')));
  62. switch ( $ext )
  63. {
  64. case '.gif':
  65. imagegif($im, $fileName);
  66. break;
  67. case '.jpg':
  68. case '.jpeg':
  69. imagejpeg($im, $fileName, $quality);
  70. break;
  71. case '.png':
  72. imagepng($im, $fileName);
  73. break;
  74. case '.bmp':
  75. imagewbmp($im, $fileName);
  76. break;
  77. default:
  78. return false;
  79. }
  80. return true;
  81. }
  82. $im = thumbnail('temp.jpg', 100);
  83. imageToFile($im, 'temp-thumbnail.jpg');
复制代码

php


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan