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 相关文章推荐
深入理解用mysql_fetch_row()以数组的形式返回查询结果
Jun 05 PHP
php/js获取客户端mac地址的实现代码
Jul 08 PHP
php警告Creating default object from empty value 问题的解决方法
Apr 02 PHP
PHP中设置一个严格30分钟过期Session面试题的4种答案
Jul 30 PHP
PHP数组相关函数汇总
Mar 24 PHP
typecho插件编写教程(五):核心代码
May 28 PHP
php将一维数组转换为每3个连续值组成的二维数组
May 06 PHP
PHP内存缓存功能memcached示例
Oct 19 PHP
ThinkPHP 在阿里云上的nginx.config配置实例详解
Oct 11 PHP
PHP实现模拟http请求的方法分析
Dec 20 PHP
PHP strripos函数用法总结
Feb 11 PHP
Yii框架视图、视图布局、视图数据块操作示例
Oct 14 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中的时间显示
2007/01/18 PHP
深入php之规范编程命名小结
2013/05/15 PHP
详解js异步文件加载器
2016/01/24 PHP
php+mysql+jquery实现日历签到功能
2017/02/27 PHP
win10 apache配置虚拟主机后localhost无法使用的解决方法
2018/01/27 PHP
vmware linux系统安装最新的php7图解
2019/04/14 PHP
JS window.opener返回父页面的应用
2009/10/24 Javascript
JavaScript Event学习第三章 早期的事件处理程序
2010/02/07 Javascript
分享几个超级震憾的图片特效
2012/01/08 Javascript
利用JS解决ie6不支持max-width,max-height问题的方法
2014/01/02 Javascript
jQuery实现鼠标划过展示大图的方法
2015/03/09 Javascript
jQuery控制DIV层实现由大到小,由远及近动画变化效果
2015/10/09 Javascript
Vue 过渡实现轮播图效果
2017/03/27 Javascript
nodejs个人博客开发第三步 载入页面
2017/04/12 NodeJs
laydate 显示结束时间不小于开始时间的实例
2017/08/11 Javascript
Vue 表单控件绑定的实现示例
2017/08/11 Javascript
原生JS实现的双色球功能示例
2018/02/02 Javascript
vue.js 底部导航栏 一级路由显示 子路由不显示的解决方法
2018/03/09 Javascript
js实现石头剪刀布游戏
2020/10/11 Javascript
[05:23]DOTA2-DPC中国联赛2月1日Recap集锦
2021/03/11 DOTA
详解Python的Django框架中的模版继承
2015/07/16 Python
Python内置模块turtle绘图详解
2017/12/09 Python
Python字典中的键映射多个值的方法(列表或者集合)
2018/10/17 Python
python os.fork() 循环输出方法
2019/08/08 Python
python3文件复制、延迟文件复制任务的实现方法
2019/09/02 Python
python配置文件写入过程详解
2019/10/19 Python
tensorflow 利用expand_dims和squeeze扩展和压缩tensor维度方式
2020/02/07 Python
使用TFRecord存取多个数据案例
2020/02/17 Python
HTML5 层的叠加的实现
2020/07/07 HTML / CSS
澳大利亚免息网上购物:Shop Zero
2016/09/17 全球购物
Omio美国:全欧洲低价大巴、火车和航班搜索和比价
2017/11/08 全球购物
英国在线女鞋目的地:SIMMI
2018/12/27 全球购物
护士节活动总结
2014/08/29 职场文书
护士求职自荐信范文
2015/03/04 职场文书
商务司机岗位职责
2015/04/10 职场文书
nginx location中多个if里面proxy_pass的方法
2021/03/31 Servers