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随机生成福彩双色球号码的2种方法
Feb 04 PHP
Linux编译升级php的详细方法
Nov 04 PHP
PHP字符串的连接的简单实例
Dec 30 PHP
PHP操作文件的一些基本函数使用示例
Nov 18 PHP
ThinkPHP处理Ajax返回的方法
Nov 22 PHP
php5.3不能连接mssql数据库的解决方法
Dec 27 PHP
mysql alter table命令修改表结构实例详解
Sep 24 PHP
ThinkPHP Where 条件中常用表达式示例(详解)
Mar 31 PHP
Laravel中如何增加自定义全局函数详解
May 09 PHP
PHP实现文件上传功能实例代码
May 18 PHP
Thinkphp 在api开发中异常返回依然是html的解决方式
Oct 16 PHP
PHP pthreads v3使用中的一些坑和注意点分析
Feb 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
西德产收音机
2021/03/01 无线电
PHP如何得到当前页和上一页的地址?
2006/11/27 PHP
php入门教程 精简版
2009/12/13 PHP
一个经典的PHP验证码类分享
2014/11/18 PHP
如何通过Linux命令行使用和运行PHP脚本
2015/07/29 PHP
redis+php实现微博(二)发布与关注功能详解
2019/09/23 PHP
php实现银联商务公众号+服务窗支付的示例代码
2019/10/12 PHP
js验证表单第二部分
2006/11/25 Javascript
IE与Firefox下javascript getyear年份的兼容性写法
2007/12/20 Javascript
jquery中的$(document).ready()与window.onload的区别
2009/11/18 Javascript
判断用户的在线状态 onbeforeunload事件
2011/03/05 Javascript
jquery 与NVelocity 产生冲突的解决方法
2011/06/13 Javascript
Extjs中ComboBox加载并赋初值的实现方法
2012/03/22 Javascript
THREE.JS入门教程(1)THREE.JS使用前了解
2013/01/24 Javascript
jQuery实现的网页左侧在线客服效果代码
2015/10/23 Javascript
基于Jquery easyui 选中特定的tab
2015/11/17 Javascript
网页前端登录js按Enter回车键实现登陆的两种方法
2016/05/10 Javascript
jQuery实现页面点击后退弹出提示框的方法
2016/08/24 Javascript
Angular2使用Angular CLI快速搭建工程(一)
2017/05/21 Javascript
详细分析单线程JS执行问题
2017/11/22 Javascript
vue自定义tap指令及tap事件的实现
2018/09/18 Javascript
微信小程序 wxParse插件显示视频问题
2019/09/27 Javascript
JavaScript中window和document用法详解
2020/07/28 Javascript
Django使用Mysql数据库已经存在的数据表方法
2018/05/27 Python
Python进阶:生成器 懒人版本的迭代器详解
2019/06/29 Python
Python叠加两幅栅格图像的实现方法
2019/07/05 Python
python3 selenium自动化 下拉框定位的例子
2019/08/23 Python
Django项目创建到启动详解(最全最详细)
2019/09/07 Python
Python drop方法删除列之inplace参数实例
2020/06/27 Python
python实现银行账户系统
2021/02/22 Python
医院门卫岗位职责
2013/12/30 职场文书
我们的节日端午节活动方案
2014/03/02 职场文书
锦旗标语大全
2014/06/23 职场文书
python爬不同图片分别保存在不同文件夹中的实现
2021/04/02 Python
详解JS数组方法
2021/11/20 Javascript
Vue+Flask实现图片传输功能
2022/04/01 Vue.js