怎么在PHP中使用imagick生成一个组合缩略图

发布时间:2020-12-25 16:22:18 作者:Leah
来源:亿速云 阅读:155

怎么在PHP中使用imagick生成一个组合缩略图?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

复制代码 代码如下:


sudo pecl install imagick

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

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

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

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

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

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

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

5.更多张图片,则只取前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生成一个组合缩略图问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. PHP通过imagick生成PSD文件缩略图的方法
  2. 怎么在PHP中自动生成缩略图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php imagick imag

上一篇:使用php怎么判断服务器是否支持Gzip压缩

下一篇:使用PHP怎么访问带密码的Redis数据库

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》