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 相关文章推荐
对javascript和select部件的结合运用
Oct 09 PHP
php 服务器调试 Zend Debugger 的安装教程
Sep 25 PHP
分享一个PHP数据流应用的简单例子
Jun 01 PHP
基于PHP中的常用函数回顾
Jul 11 PHP
PHP中include与require使用方法区别详解
Oct 19 PHP
PHP中4个加速、缓存扩展的区别和选用建议
Mar 12 PHP
PHP中使用file_get_contents抓取网页中文乱码问题解决方法
Dec 17 PHP
PHP学习笔记(三):数据类型转换与常量介绍
Apr 17 PHP
php遍历类中包含的所有元素的方法
May 12 PHP
Ecshop 后台添加新功能栏目及管理权限设置教程
Nov 21 PHP
php微信公众号开发之快递查询
Oct 20 PHP
使用ucenter实现多站点同步登录的讲解
Mar 21 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 获取本机外网/公网IP的代码
2010/05/09 PHP
PHP实现linux命令tail -f
2016/02/22 PHP
Laravel中任务调度console使用方法小结
2017/05/07 PHP
jQuery 处理表单元素的代码
2010/02/15 Javascript
jsp网页搜索结果中实现选中一行使其高亮
2014/02/17 Javascript
深入理解javascript构造函数和原型对象
2014/09/23 Javascript
jQuery中is()方法用法实例
2015/01/06 Javascript
jQuery构造函数init参数分析
2015/05/13 Javascript
asp.net中oracle 存储过程(图文)
2015/08/12 Javascript
JavaScript识别网页关键字并进行描红的方法
2015/11/09 Javascript
详解Vue2+Echarts实现多种图表数据可视化Dashboard(附源码)
2017/03/21 Javascript
JavaScript自执行函数和jQuery扩展方法详解
2017/10/27 jQuery
JavaScript实用代码小技巧
2018/08/23 Javascript
图文讲解用vue-cli脚手架创建vue项目步骤
2019/02/12 Javascript
详解如何使用router-link对象方式传递参数?
2019/05/02 Javascript
JavaScript判断浏览器运行环境的详细方法
2019/06/30 Javascript
Vue配置marked链接添加target="_blank"的方法
2019/07/19 Javascript
[01:45]DOTA2众星出演!DSPL刀塔次级职业联赛宣传片
2014/11/21 DOTA
Python urllib模块urlopen()与urlretrieve()详解
2013/11/01 Python
使用Python实现BT种子和磁力链接的相互转换
2015/11/09 Python
python下如何查询CS反恐精英的服务器信息
2017/01/17 Python
Python基于回溯法子集树模板解决取物搭配问题实例
2017/09/02 Python
Python内置函数——__import__ 的使用方法
2017/11/24 Python
python 拷贝特定后缀名文件,并保留原始目录结构的实例
2018/04/27 Python
基于python批量处理dat文件及科学计算方法详解
2018/05/08 Python
如何基于Python批量下载音乐
2019/11/11 Python
python 项目目录结构设置
2020/02/14 Python
在Python中通过threshold创建mask方式
2020/02/19 Python
Django实现将views.py中的数据传递到前端html页面,并展示
2020/03/16 Python
浅谈Python __init__.py的作用
2020/10/28 Python
保险专业自荐信范文
2014/02/20 职场文书
外贸专业求职信
2014/03/09 职场文书
小学清明节活动总结
2014/07/04 职场文书
2014年教务处工作总结
2014/12/03 职场文书
教育实习指导教师评语
2014/12/31 职场文书
员工辞职信怎么写
2015/02/27 职场文书