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张,则仅截取前4张,并按田字格方式生成缩略图。
虽然规则看似不少,但逻辑并不复杂,很快便实现了上述功能:
namespace \clarence\thumbnail;
class Thumbnail extends \Imagick
{
/**
* @param array $images
* @param int $width
* @param int $height
* @return static
* @throws ThumbnailException
*/
public static function createFromImages($images, $width, $height){
if (empty($images)){
throw new ThumbnailException("No images!");
}
$thumbnail = new static();
$thumbnail->newImage($width, $height, 'white', 'jpg');
$thumbnail->compositeImages($images);
return $thumbnail;
}
public function compositeImages($images){
$imagesKeys = array_keys($images);
$compositeConfig = $this->calcCompositeImagesPosAndSize($images);
foreach ($compositeConfig as $index => $cfg){
$imgKey = $imagesKeys[$index];
$img = new \Imagick($images[$imgKey]);
$img = $this->makeCompositeThumbnail($img, $cfg);
$this->compositeImage($img, self::COMPOSITE_OVER, $cfg['to']['x'], $cfg['to']['y']);
}
}
protected function makeCompositeThumbnail(\Imagick $img, $cfg){
$img->cropThumbnailImage($cfg['size']['width'], $cfg['size']['height']);
return $img;
}
protected function calcCompositeImagesPosAndSize($images){
$width = $this->getImageWidth();
$height = $this->getImageHeight();
switch(count($images)){
case 0:
throw new ThumbnailException("No images!");
case 1:
// | 0 |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width,
'height' => $height,
]
]
];
case 2:
// | 0 | 1 |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width / 2,
'height' => $height,
]
],
1 => [
'to' => [ 'x' => $width / 2, 'y' => 0],
'size' => [
'width' => $width / 2,
'height' => $height,
]
]
];
case 3:
// | 0 | 1 |
// | 2 | |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
1 => [
'to' => [ 'x' => $width / 2, 'y' => 0],
'size' => [
'width' => $width / 2,
'height' => $height,
]
],
2 => [
'to' => [ 'x' => 0, 'y' => $height / 2 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
];
default:
// >= 4:
// | 0 | 1 |
// | 2 | 3 |
return [
0 => [
'to' => [ 'x' => 0, 'y' => 0 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
1 => [
'to' => [ 'x' => $width / 2, 'y' => 0],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
2 => [
'to' => [ 'x' => 0, 'y' => $height / 2 ],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
3 => [
'to' => [ 'x' => $width / 2, 'y' => $height / 2],
'size' => [
'width' => $width / 2,
'height' => $height / 2,
]
],
];
}
}
}
下面是一个简单的使用示例:
复制代码 代码如下: $thumbnail = \clarence\thumbnail\Thumbnail::createFromImages($srcImages, 240, 320); $thumbnail->writeImage($outputDir."/example.jpg");
以上内容介绍了基于 PHP 和 imagick 扩展生成组合缩略图的方法,希望对相关开发者有所帮助。