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数据库转移,access,sql server 转 MySQL 的图文教程
Sep 02 PHP
php简单静态页生成过程
Mar 27 PHP
php获得用户ip地址的比较不错的方法
Feb 08 PHP
PHP取余函数介绍MOD(x,y)与x%y
May 15 PHP
PHP在innodb引擎下快速代建全文搜索功能简明教程【基于xunsearch】
Oct 14 PHP
PHP使用SWOOLE扩展实现定时同步 MySQL 数据
Apr 09 PHP
PHP编程计算日期间隔天数的方法
Apr 26 PHP
Yii框架扩展CGridView增加导出CSV功能的方法
May 24 PHP
PHP面向对象之工作单元(实例讲解)
Jun 26 PHP
php框架CodeIgniter主从数据库配置方法分析
May 25 PHP
php生成HTML文件的类方法
Oct 11 PHP
tp5.1 框架查询表达式用法详解
May 25 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
对Session和Cookie的区分与解释
2007/03/16 PHP
解决PHP在DOS命令行下却无法链接MySQL的技术笔记
2010/12/29 PHP
PHP获取中英混合字符串长度的方法
2014/06/07 PHP
php获取QQ头像并显示的方法
2014/12/23 PHP
简单谈谈php延迟静态绑定
2016/01/26 PHP
php短信接口代码
2016/05/13 PHP
详解Laravel5.6 Passport实现Api接口认证
2018/07/27 PHP
JavaScript中String和StringBuffer的速度之争
2010/04/01 Javascript
Jquery Ajax xmlhttp请求成功问题
2015/02/04 Javascript
touch.js 拖动、缩放、旋转 (鼠标手势)功能代码
2017/02/04 Javascript
js实现打地鼠小游戏
2017/02/13 Javascript
ES6新数据结构Map功能与用法示例
2017/03/31 Javascript
jQuery实现的3D版图片轮播示例【滑动轮播】
2019/01/18 jQuery
javascript实现小型区块链功能
2019/04/03 Javascript
微信小程序图片左右摆动效果详解
2019/07/13 Javascript
JavaScript实现PC端横向轮播图
2020/02/07 Javascript
[01:02:18]VGJ.S vs infamous Supermajor 败者组 BO3 第一场 6.4
2018/06/05 DOTA
利用soaplib搭建webservice详细步骤和实例代码
2013/11/20 Python
简单介绍Python的轻便web框架Bottle
2015/04/08 Python
示例详解Python3 or Python2 两者之间的差异
2018/08/23 Python
Python字典创建 遍历 添加等实用基础操作技巧
2018/09/13 Python
对python数据切割归并算法的实例讲解
2018/12/12 Python
解决python的空格和tab混淆而报错的问题
2021/02/26 Python
CSS3 新增选择器的实例
2019/11/13 HTML / CSS
html5+css如何实现中间大两头小的轮播效果
2018/12/06 HTML / CSS
萨克斯第五大道英国:Saks Fifth Avenue英国
2019/04/01 全球购物
土耳其玩具商店:Toyzz Shop
2019/08/02 全球购物
Oasis服装官网:时尚女装在线
2020/07/09 全球购物
优秀大学生推荐信范文
2013/11/28 职场文书
公司踏青活动方案
2014/08/16 职场文书
四风问题个人自查剖析材料思想汇报
2014/09/21 职场文书
2014大学生党员评议个人总结
2014/09/22 职场文书
财务负责人岗位职责
2015/02/03 职场文书
出纳岗位职责范本
2015/03/31 职场文书
nginx优化的六点方法
2021/03/31 Servers
react合成事件与原生事件的相关理解
2021/05/13 Javascript