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脚本的10个技巧(1)
Oct 09 PHP
PHPWind 发帖回帖Api PHP版打包下载
Feb 08 PHP
php中函数的形参与实参的问题说明
Sep 01 PHP
分享php分页的功能模块
Jun 16 PHP
php结合curl实现多线程抓取
Jul 09 PHP
php链表用法实例分析
Jul 09 PHP
详谈PHP编码转换问题
Jul 28 PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
Dec 02 PHP
如何使用微信公众平台开发模式实现多客服
Jan 06 PHP
详解PHP原生DOM对象操作XML的方法
Oct 17 PHP
浅谈php中curl、fsockopen的应用
Dec 10 PHP
如何用PHP实现分布算法之一致性哈希算法
May 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
PHP实现搜索相似图片
2015/09/22 PHP
PHP+apc+ajax实现的ajax_upload上传进度条代码
2016/01/25 PHP
PHP实现验证码校验功能
2017/11/16 PHP
JQuery从头学起第二讲
2010/07/04 Javascript
Javascript this 的一些学习总结
2012/08/31 Javascript
关于JavaScript的面向对象和继承有利新手学习
2013/01/11 Javascript
javascript操作referer详细解析
2014/03/10 Javascript
jQuery+css实现的蓝色水平二级导航菜单效果代码
2015/09/11 Javascript
javascript基于prototype实现类似OOP继承的方法
2015/12/16 Javascript
jQuery使用each方法与for语句遍历数组示例
2016/06/16 Javascript
jQuery简单实现彩色云标签效果示例
2016/08/01 Javascript
简洁实用的BootStrap jQuery手风琴插件
2016/08/31 Javascript
AngularJS监听路由的变化示例代码
2016/09/23 Javascript
JavaScript设计模式之缓存代理模式原理与简单用法示例
2018/08/07 Javascript
vue项目中在外部js文件中直接调用vue实例的方法比如说this
2019/04/28 Javascript
vue-cli脚手架打包静态资源请求出错的原因与解决
2019/06/06 Javascript
jquery ui 实现 tab标签功能示例【测试可用】
2019/07/25 jQuery
Element Dropdown下拉菜单的使用方法
2020/07/26 Javascript
Python3enumrate和range对比及示例详解
2019/07/13 Python
Python的Lambda函数用法详解
2019/09/03 Python
tensorflow之获取tensor的shape作为max_pool的ksize实例
2020/01/04 Python
python turtle工具绘制四叶草的实例分享
2020/02/14 Python
python numpy库linspace相同间隔采样的实现
2020/02/25 Python
python手机号前7位归属地爬虫代码实例
2020/03/31 Python
亿阳信通股份有限公司笔试题(C#)
2016/03/04 面试题
工程造价专业大专生求职信
2013/10/06 职场文书
汽车维修专业个人求职信范文
2014/01/01 职场文书
电脑租赁公司创业计划书
2014/01/08 职场文书
颁奖典礼主持词
2014/03/25 职场文书
培训班主持词
2014/03/28 职场文书
求职信名称怎么写
2014/05/26 职场文书
八一建军节营销活动方案
2014/08/31 职场文书
2015年清明节网上祭英烈留言寄语
2015/03/04 职场文书
写作技巧:优秀文案必备的3种结构
2019/08/19 职场文书
Java 超详细讲解ThreadLocal类的使用
2022/04/07 Java/Android
Linux在两个服务器直接传文件的操作方法
2022/08/05 Servers