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 相关文章推荐
Adodb的十个实例(清晰版)
Dec 31 PHP
PHP5.3的垃圾回收机制(动态存储分配方案)深入理解
Dec 10 PHP
利用PHP扩展vld查看PHP opcode操作步骤
Mar 04 PHP
浅析php变量修饰符static的使用
Jun 28 PHP
用 Composer构建自己的 PHP 框架之设计 MVC
Oct 30 PHP
php递归json类实例
Dec 02 PHP
PHP入门教程之日期与时间操作技巧总结(格式化,验证,获取,转换,计算等)
Sep 11 PHP
PHP中in_array函数使用的问题与解决办法
Sep 11 PHP
一个简单安全的PHP验证码类、PHP验证码
Sep 24 PHP
PHP简单实现循环链表功能示例
Nov 10 PHP
PHP addslashes()函数讲解
Feb 03 PHP
浅谈PHP封装CURL
Mar 06 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
PHP4实际应用经验篇(9)
2006/10/09 PHP
一个好用的分页函数
2006/11/16 PHP
解析获取优酷视频真实下载地址的PHP源代码
2013/06/26 PHP
php 验证码(倾斜,正弦干扰线,黏贴,旋转)
2013/06/29 PHP
php模板原理讲解
2013/11/13 PHP
ThinkPHP中公共函数路径和配置项路径的映射分析
2014/11/22 PHP
PHP简单实现数字分页功能示例
2016/08/24 PHP
PHP登录(ajax提交数据和后台校验)实例分享
2016/12/29 PHP
对laravel的session获取与存取方法详解
2019/10/08 PHP
javascript中的array数组使用技巧
2010/01/31 Javascript
js时间戳格式化成日期格式的多种方法
2013/11/11 Javascript
简单谈谈JavaScript的同步与异步
2015/12/31 Javascript
原生JS实现垂直手风琴效果
2017/02/19 Javascript
js使用Promise实现简单的Ajax缓存
2018/11/14 Javascript
基于vue+element实现全局loading过程详解
2020/07/10 Javascript
原生JS实现pc端轮播图效果
2020/12/21 Javascript
[01:32:10]NAVI vs VG Supermajor 败者组 BO3 第一场 6.5
2018/06/06 DOTA
Python随机生成一个6位的验证码代码分享
2015/03/24 Python
以Python的Pyspider为例剖析搜索引擎的网络爬虫实现方法
2015/03/30 Python
讲解Python中的标识运算符
2015/05/14 Python
scrapy spider的几种爬取方式实例代码
2018/01/25 Python
python 二维矩阵转三维矩阵示例
2019/11/30 Python
Python输出指定字符串的方法
2020/02/06 Python
iPython pylab模式启动方式
2020/04/24 Python
关于Python3的import问题(pycharm可以运行命令行import错误)
2020/11/18 Python
荷兰DOD药房中文官网:DeOnlineDrogist
2020/12/27 全球购物
Java里面StringBuilder和StringBuffer有什么区别
2016/06/06 面试题
介绍一下Linux内核的排队自旋锁
2014/01/04 面试题
英语系本科生求职信范文
2013/12/18 职场文书
关于期中考试的反思
2014/02/02 职场文书
医德医魂心得体会
2014/09/11 职场文书
在宿舍喝酒的检讨书
2014/09/28 职场文书
人身意外保险授权委托书
2014/10/01 职场文书
2014年办公室人员工作总结
2014/12/09 职场文书
专业技术人员年度考核评语
2014/12/31 职场文书
毕业生就业推荐表导师评语
2014/12/31 职场文书