PHP利用imagick生成组合缩略图


Posted in PHP onFebruary 19, 2016

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

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 (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生成组合缩略图的相关知识,希望对大家有所帮助!

PHP 相关文章推荐
PHP与MySQL开发中页面乱码的产生与解决
Mar 27 PHP
php递归删除目录下的文件但保留的实例分享
May 10 PHP
从零开始学YII2框架(三)扩展插件yii2-gird
Aug 20 PHP
Thinkphp调用Image类生成缩略图的方法
Mar 07 PHP
Session 失效的原因汇总及解决丢失办法
Sep 30 PHP
PHP+jQuery翻板抽奖功能实现
Oct 19 PHP
php短信接口代码
May 13 PHP
PHPStorm+XDebug进行调试图文教程
Jun 13 PHP
微信随机生成红包金额算法php版
Jul 21 PHP
PHP自定义序列化接口Serializable用法分析
Dec 29 PHP
基于laravel where的高级使用方法
Oct 10 PHP
PHP函数用法详解【初始化、嵌套、内置函数等】
Jun 02 PHP
对比分析php中Cookie与Session的异同
Feb 19 #PHP
php强大的时间转换函数strtotime
Feb 18 #PHP
php实现中文转数字
Feb 18 #PHP
PHP和MySql中32位和64位的整形范围是多少
Feb 18 #PHP
php脚本运行时的超时机制详解
Feb 17 #PHP
PHP邮件群发机实现代码
Feb 16 #PHP
46 个非常有用的 PHP 代码片段
Feb 16 #PHP
You might like
PHP 远程关机实现代码
2009/11/10 PHP
laravel ORM 只开启created_at的几种方法总结
2018/01/29 PHP
php连接sftp的作用以及实例代码
2019/09/23 PHP
css图片自适应大小
2007/11/28 Javascript
20个非常有用的PHP类库 加速php开发
2010/01/15 Javascript
Jquery + Ajax调用webService实例代码(asp.net)
2010/08/27 Javascript
JavaScript检测实例属性, 原型属性
2015/02/04 Javascript
包含中国城市的javascript对象实例
2015/08/03 Javascript
如何使用Bootstrap 按钮实例详解
2017/03/29 Javascript
微信小程序页面开发注意事项整理
2017/05/18 Javascript
基于bootstrap实现多个下拉框同时搜索功能
2017/07/19 Javascript
微信小程序组件传值图示过程详解
2019/07/31 Javascript
微信小程序实现可拖动悬浮图标(包括按钮角标的实现)
2020/12/29 Javascript
python变量不能以数字打头详解
2016/07/06 Python
利用python获取Ping结果示例代码
2017/07/06 Python
Python基于xlrd模块操作Excel的方法示例
2018/06/21 Python
如何基于python操作excel并获取内容
2019/12/24 Python
python pycharm最新版本激活码(永久有效)附python安装教程
2020/09/18 Python
Python enumerate内置库用法解析
2020/02/24 Python
python GUI库图形界面开发之PyQt5输入对话框QInputDialog详细使用方法与实例
2020/02/27 Python
GDAL 矢量属性数据修改方式(python)
2020/03/10 Python
Python调用SMTP服务自动发送Email的实现步骤
2021/02/07 Python
整理HTML5的一些新特性与Canvas的常用属性
2016/01/29 HTML / CSS
Shopee马来西亚:随拍即卖,最佳行动电商拍卖平台
2017/06/05 全球购物
美国亚洲时尚和美容产品的一站式网上商店:Stylevana
2019/09/05 全球购物
Currentbody澳大利亚:美容仪专家
2019/11/11 全球购物
what is the difference between ext2 and ext3
2015/08/25 面试题
Java中会存在内存泄漏吗,请简单描述
2016/12/22 面试题
2013年员工自我评价范文
2013/12/27 职场文书
车间统计员岗位职责
2014/01/05 职场文书
电子信息科学专业自荐信
2014/01/30 职场文书
公司年会抽奖活动主持词
2014/03/31 职场文书
2015年办公室个人工作总结
2015/04/20 职场文书
高中生综合素质评价范文
2015/08/18 职场文书
经典格言警句:没有热忱,世间便无进步
2019/11/13 职场文书
python中opencv实现图片文本倾斜校正
2021/06/11 Python