PHP利用imagick生成组合缩略图

这里说的imagick 是 ImageMagick 在PHP下的扩展,本文给大家介绍PHP利用imagick生成组合缩略图,需要的朋友参考下。

先给大家炫下效果图,如果大家觉得还很满意,请继续往下阅读:

这里说的imagick 是 ImageMagick 在PHP下的扩展,使用pecl安装起来那叫一个轻松简单一条命令就搞定:

sudo pecl install imagick

(扩展装好后还是要在php.ini中加上extension=imagick.so,然后记得重启apache或php-fpm服务。)

最近有个需求是要把多张图片组合起来生成缩略图,刚好用用这个强大的imagick扩展。

这个需求是要这样生成缩略图:

1.如果有1张图片,就直接生成这张图片的缩略图;

2.如果有2张图片,则一张在左边一张在右边,各一半;

3.如果有3张图片,则两张左边平均分配,一张独占右边;

4.如果有4张图片,则像田字格一样平均分配空间;

5.更多张图片,则只取前4张,按田字格方式生成缩略图。

这规则还真不少,不过还不算太过复杂,很快搞出来了:

  1. namespace \clarence\thumbnail;
  2. class Thumbnail extends \Imagick
  3. {
  4. /**
  5. * @param array $images
  6. * @param int $width
  7. * @param int $height
  8. * @return static
  9. * @throws ThumbnailException
  10. */
  11. public static function createFromImages($images, $width, $height){
  12. if (emptyempty($images)){
  13. throw new ThumbnailException("No images!");
  14. }
  15. $thumbnail = new static();
  16. $thumbnail->newImage($width, $height, 'white', 'jpg');
  17. $thumbnail->compositeImages($images);
  18. return $thumbnail;
  19. }
  20. public function compositeImages($images){
  21. $imagesKeys = array_keys($images);
  22. $compositeConfig = $this->calcCompositeImagesPosAndSize($images);
  23. foreach ($compositeConfig as $index => $cfg){
  24. $imgKey = $imagesKeys[$index];
  25. $img = new \Imagick($images[$imgKey]);
  26. $img = $this->makeCompositeThumbnail($img, $cfg);
  27. $this->compositeImage($img, self::COMPOSITE_OVER, $cfg['to']['x'], $cfg['to']['y']);
  28. }
  29. }
  30. protected function makeCompositeThumbnail(\Imagick $img, $cfg){
  31. $img->cropThumbnailImage($cfg['size']['width'], $cfg['size']['height']);
  32. return $img;
  33. }
  34. protected function calcCompositeImagesPosAndSize($images){
  35. $width = $this->getImageWidth();
  36. $height = $this->getImageHeight();
  37. switch(count($images)){
  38. case 0:
  39. throw new ThumbnailException("No images!");
  40. case 1:
  41. // | 0 |
  42. return [
  43. 0 => [
  44. 'to' => [ 'x' => 0, 'y' => 0 ],
  45. 'size' => [
  46. 'width' => $width,
  47. 'height' => $height,
  48. ]
  49. ]
  50. ];
  51. case 2:
  52. // | 0 | 1 |
  53. return [
  54. 0 => [
  55. 'to' => [ 'x' => 0, 'y' => 0 ],
  56. 'size' => [
  57. 'width' => $width / 2,
  58. 'height' => $height,
  59. ]
  60. ],
  61. 1 => [
  62. 'to' => [ 'x' => $width / 2, 'y' => 0],
  63. 'size' => [
  64. 'width' => $width / 2,
  65. 'height' => $height,
  66. ]
  67. ]
  68. ];
  69. case 3:
  70. // | 0 | 1 |
  71. // | 2 | |
  72. return [
  73. 0 => [
  74. 'to' => [ 'x' => 0, 'y' => 0 ],
  75. 'size' => [
  76. 'width' => $width / 2,
  77. 'height' => $height / 2,
  78. ]
  79. ],
  80. 1 => [
  81. 'to' => [ 'x' => $width / 2, 'y' => 0],
  82. 'size' => [
  83. 'width' => $width / 2,
  84. 'height' => $height,
  85. ]
  86. ],
  87. 2 => [
  88. 'to' => [ 'x' => 0, 'y' => $height / 2 ],
  89. 'size' => [
  90. 'width' => $width / 2,
  91. 'height' => $height / 2,
  92. ]
  93. ],
  94. ];
  95. default:
  96. // >= 4:
  97. // | 0 | 1 |
  98. // | 2 | 3 |
  99. return [
  100. 0 => [
  101. 'to' => [ 'x' => 0, 'y' => 0 ],
  102. 'size' => [
  103. 'width' => $width / 2,
  104. 'height' => $height / 2,
  105. ]
  106. ],
  107. 1 => [
  108. 'to' => [ 'x' => $width / 2, 'y' => 0],
  109. 'size' => [
  110. 'width' => $width / 2,
  111. 'height' => $height / 2,
  112. ]
  113. ],
  114. 2 => [
  115. 'to' => [ 'x' => 0, 'y' => $height / 2 ],
  116. 'size' => [
  117. 'width' => $width / 2,
  118. 'height' => $height / 2,
  119. ]
  120. ],
  121. 3 => [
  122. 'to' => [ 'x' => $width / 2, 'y' => $height / 2],
  123. 'size' => [
  124. 'width' => $width / 2,
  125. 'height' => $height / 2,
  126. ]
  127. ],
  128. ];
  129. }
  130. }
  131. }

用个试试:

$thumbnail = \clarence\thumbnail\Thumbnail::createFromImages($srcImages, 240, 320);

$thumbnail->writeImage($outputDir."/example.jpg");

以上内容给大家介绍了PHP利用imagick生成组合缩略图的相关知识,希望对大家有所帮助!