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 相关文章推荐
收集的DedeCMS一些使用经验
Mar 17 PHP
PHP IPV6正则表达式验证代码
Feb 16 PHP
PHP中把stdClass Object转array的几个方法
May 08 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
Mar 19 PHP
PHP中使用socket方式GET、POST数据实例
Apr 02 PHP
php获取图片信息的方法详解
Dec 10 PHP
Zend Framework教程之模型Model基本规则和使用方法
Mar 04 PHP
PHP函数引用返回的实例详解
Sep 11 PHP
PHP正则替换函数preg_replace()报错:Notice Use of undefined constant的解决方法分析
Feb 04 PHP
PHP编程中的Session阻塞问题与解决方法分析
Aug 07 PHP
PHP面向对象程序设计重载(overloading)操作详解
Jun 13 PHP
php+js实现的拖动滑块验证码验证表单操作示例【附源码下载】
May 27 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
Windows和Linux中php代码调试工具Xdebug的安装与配置详解
2014/05/08 PHP
Zend Framework数据库操作技巧总结
2017/02/18 PHP
thinkPHP5.0框架命名空间详解
2017/03/18 PHP
javascript 可以拖动的DIV(二)
2009/06/26 Javascript
JS延迟加载(setTimeout) JS最后加载
2010/07/15 Javascript
JavaScript常用标签和方法总结
2015/09/01 Javascript
Bootstrap响应式侧边栏改进版
2016/09/17 Javascript
vue2导航根据路由传值,而改变导航内容的实例
2017/11/10 Javascript
详解Vue底部导航栏组件
2019/05/02 Javascript
使用Python的Flask框架构建大型Web应用程序的结构示例
2016/06/04 Python
python append、extend与insert的区别
2016/10/13 Python
python查看微信好友是否删除自己
2016/12/19 Python
浅谈Django REST Framework限速
2017/12/12 Python
详谈Python中列表list,元祖tuple和numpy中的array区别
2018/04/18 Python
python docx 中文字体设置的操作方法
2018/05/08 Python
浅谈python的输入输出,注释,基本数据类型
2019/04/02 Python
python中使用while循环的实例
2019/08/05 Python
python3应用windows api对后台程序窗口及桌面截图并保存的方法
2019/08/27 Python
Python动态导入模块:__import__、importlib、动态导入的使用场景实例分析
2020/03/30 Python
浅谈keras使用中val_acc和acc值不同步的思考
2020/06/18 Python
Python私有属性私有方法应用实例解析
2020/09/15 Python
Html5页面中的返回实现的方法
2018/02/26 HTML / CSS
墨西哥运动服饰和鞋网上商店:Netshoes墨西哥
2016/07/28 全球购物
CHARLES & KEITH加拿大官网:新加坡时尚品牌
2020/03/26 全球购物
俄罗斯茶和咖啡网上商店:Tea.ru
2021/01/26 全球购物
信息管理专业学生自荐信格式
2013/09/22 职场文书
英语专业毕业生求职简历的自我评价
2013/10/24 职场文书
财务与信息服务专业推荐信
2013/11/28 职场文书
教师实习自我鉴定
2013/12/13 职场文书
门卫班长岗位职责
2013/12/15 职场文书
会计专业个人求职信范文
2014/01/08 职场文书
社团活动总结范文
2014/04/26 职场文书
珍惜资源保护环境的建议书
2014/05/14 职场文书
领导干部作风建设自查报告
2014/10/23 职场文书
电子商务专业求职信范文
2015/03/19 职场文书
淘宝客服专员岗位职责
2015/04/07 职场文书