PHP利用imagick生成组合缩略图
程序员文章站
2024-04-01 18:24:52
先给大家炫下效果图,如果大家觉得还很满意,请继续往下阅读:
这里说的imagick 是 imagemagick 在php下的扩展。使用pecl安装起来那叫一个轻松简单...
先给大家炫下效果图,如果大家觉得还很满意,请继续往下阅读:
这里说的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张,按田字格方式生成缩略图。
这规则还真不少,不过还不算太过复杂,很快搞出来了:
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生成组合缩略图的相关知识,希望对大家有所帮助!