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 相关文章推荐
特详细的PHPMYADMIN简明安装教程
Aug 01 PHP
PHP session有效期问题
Apr 26 PHP
mac下使用brew配置环境的步骤分享
May 23 PHP
php入门学习知识点三 PHP上传
Jul 14 PHP
解析CodeIgniter自定义配置文件
Jun 18 PHP
php使用pdo连接并查询sql数据库的方法
Dec 24 PHP
YII动态模型(动态表名)支持分析
Mar 29 PHP
php版微信发红包接口用法示例
Sep 23 PHP
php 判断字符串编码是utf-8 或gb2312实例
Nov 01 PHP
Thinkphp框架+Layui实现图片/文件上传功能分析
Feb 07 PHP
Centos7安装swoole扩展操作示例
Mar 26 PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
Mar 24 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
PHP5中的this,self和parent关键字详解教程
2007/03/19 PHP
php+mysql写的简单留言本实例代码
2008/07/25 PHP
从康盛产品(discuz)提取出来的模板类
2011/06/28 PHP
浅析memcache启动以及telnet命令详解
2013/06/28 PHP
destoon安全设置中需要设置可写权限的目录及文件
2014/06/21 PHP
php实现阿拉伯数字和罗马数字相互转换的方法
2015/04/17 PHP
php使用Jpgraph绘制饼状图的方法
2015/06/10 PHP
laravel5创建service provider和facade的方法详解
2016/07/26 PHP
Ubuntu 16.04下安装PHP 7过程详解
2017/03/28 PHP
PHP+Apache环境中如何隐藏Apache版本
2017/11/24 PHP
JS控制表格隔行变色
2006/06/26 Javascript
jQuery 和 CSS 的文本特效插件集锦
2014/12/12 Javascript
JavaScript实现防止网页被嵌入Frame框架的代码分享
2014/12/29 Javascript
浅谈Jquery为元素绑定事件
2015/04/27 Javascript
js 博客内容进度插件详解
2017/02/19 Javascript
js制作简单的音乐播放器的示例代码
2017/08/28 Javascript
JS实现520 表白简单代码
2018/05/21 Javascript
微信小程序日历弹窗选择器代码实例
2019/05/09 Javascript
layui问题之渲染数据表格时,仅出现10条数据的解决方法
2019/09/12 Javascript
Javascript 关于基本类型和引用类型的个人理解
2019/11/01 Javascript
[02:20]2014DOTA2西雅图邀请赛 MVP外卡赛首胜采访
2014/07/09 DOTA
python生成随机验证码(中文验证码)示例
2014/04/03 Python
python 专题九 Mysql数据库编程基础知识
2017/03/16 Python
django模型层(model)进行建表、查询与删除的基础教程
2017/11/21 Python
Python DataFrame设置/更改列表字段/元素类型的方法
2018/06/09 Python
Linux CentOS Python开发环境搭建教程
2018/11/28 Python
selenium+python环境配置教程详解
2019/05/28 Python
Windows10下 python3.7 安装 facenet的教程
2019/09/10 Python
Python pygame绘制文字制作滚动文字过程解析
2019/12/12 Python
python3 实现口罩抽签的功能
2020/03/11 Python
Python3爬虫中Selenium的用法详解
2020/07/10 Python
pycharm中选中一个单词替换所有重复单词的实现方法
2020/11/17 Python
Nixon手表英国官网:美国尼克松手表品牌
2020/02/10 全球购物
法人授权委托书范本
2014/04/04 职场文书
冬季施工防火方案
2014/05/17 职场文书
学校2014年度工作总结
2014/12/06 职场文书