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中取得image按钮传递的name值
Oct 09 PHP
PHP中date()日期函数有关参数整理
Jul 19 PHP
php强制文件下载而非在浏览器打开的自定义函数分享
May 08 PHP
深入分析PHP优化及注意事项
Jul 04 PHP
Laravel 5.4向IoC容器中添加自定义类的方法示例
Aug 15 PHP
PHP实现非阻塞模式的方法分析
Jul 26 PHP
PHP删除字符串中非字母数字字符方法总结
Jan 20 PHP
PHP反射实际应用示例
Apr 03 PHP
一文掌握PHP Xdebug 本地与远程调试(小结)
Apr 23 PHP
YII框架行为behaviors用法示例
Apr 26 PHP
PHP __call()方法实现委托示例
May 20 PHP
PHP设计模式入门之迭代器模式原理与实现方法分析
Apr 26 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
浅析ThinkPHP中的pathinfo模式和URL重写
2014/01/06 PHP
PHP直接修改表内容DataGrid功能实现代码
2015/09/24 PHP
PHP查询附近的人及其距离的实现方法
2016/05/11 PHP
ThinkPHP框架实现FTP图片上传功能示例
2019/04/08 PHP
简明json介绍
2008/09/28 Javascript
修改jQuery.Autocomplete插件 支持中文输入法 避免TAB、ENTER键失效、导致表单提交
2009/10/11 Javascript
我的javascript 函数链之演变
2011/04/07 Javascript
js循环改变div颜色具体方法
2013/06/25 Javascript
jQuery实现将页面上HTML标签换成另外标签的方法
2015/06/09 Javascript
jquery实现仿JqueryUi可拖动的DIV实例
2015/07/31 Javascript
jQuery实现可高亮显示的二级CSS菜单效果
2015/09/01 Javascript
Angular 路由route实例代码
2016/07/12 Javascript
获取当前月(季度/年)的最后一天(set相关操作及应用)
2016/12/27 Javascript
NodeJs的fs读写删除移动监听
2017/04/28 NodeJs
vue 根据数组中某一项的值进行排序的方法
2018/08/30 Javascript
webpack优化之代码分割与公共代码提取详解
2019/11/22 Javascript
Vue中函数防抖节流的理解及应用实现
2020/04/24 Javascript
用vue写一个日历
2020/11/02 Javascript
[01:50]WODOTA制作 DOTA2中文宣传片《HERO》
2013/04/28 DOTA
Python学习之asyncore模块用法实例教程
2014/09/29 Python
Python 冒泡,选择,插入排序使用实例
2015/02/05 Python
Python脚本实现自动将数据库备份到 Dropbox
2017/02/06 Python
Python FTP两个文件夹间的同步实例代码
2018/05/25 Python
django 将model转换为字典的方法示例
2018/10/16 Python
python实现简单的文字识别
2018/11/27 Python
根据tensor的名字获取变量的值方式
2020/01/04 Python
python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
2020/02/28 Python
HTML5实现无刷新修改URL的方法
2019/11/14 HTML / CSS
Volcom法国官网:美国冲浪滑板品牌
2017/05/25 全球购物
毕业生怎样写好自荐信
2013/11/11 职场文书
工地门卫岗位职责
2013/12/30 职场文书
简历上的自我评价
2014/02/03 职场文书
干部作风建设个人剖析材料
2014/10/11 职场文书
校园会短篇的广播稿
2014/10/21 职场文书
研究生个人学年总结
2015/02/14 职场文书
应届毕业生的自我评价
2019/06/21 职场文书