-
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张,按田字格方式生成缩略图。
这规则还真不少,不过还不算太过复杂,很快搞出来了:
- 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 (emptyempty($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生成组合缩略图的相关知识,希望对大家有所帮助!
原文链接:http://www.phpfensi.com/php/20210710/17155.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式