php教程|php手册
imagemagick
php教程-php手册
PHP下使用强大的imagick轻松生成组合缩略图,imagick缩略图
基于sql文件管理系统源码,Ubuntu鼠标不停抖动,爬虫ajax跨域,mcpack php,seo基数知识lzw
project: blogtarget: use-imagick-to-composite-images-thumbnail.mddate: -02-19status: publishtags: - php - imagick - thumbnailcategories: - php
这里说的imagick
是ImageMagick
在PHP下的扩展。使用pecl
安装起来那叫一个轻松简单 —— 一条命令就搞定:
淘宝网店交易平台源码,ubuntu修改文件只读,tomcat7跨域解决,爬虫考研单词,php在网页上显示时间,建瓯seo托管lzw
sudo pecl install imagick
(扩展装好后还是要在php.ini中加上extension=imagick.so
,然后记得重启apache
或php-fpm
服务。)
源码之家登录页模板,ubuntu 重刷系统吗,tomcat5允许跨域,bt爬虫监听地址,PHP语言的特性是什么,SEO金牌会员lzw
最近有个需求是要把多张图片组合起来生成缩略图,刚好用用这个强大的imagick
扩展。
这个需求是要这样生成缩略图:
这规则还真不少,不过还不算太过复杂,很快搞出来了:
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[ o][x], $cfg[ o][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 => [ o => [ x => 0, y => 0 ], size => [ width => $width, height => $height, ] ]]; case 2:// | 0 | 1 |return [ 0 => [ o => [ x => 0, y => 0 ], size => [ width => $width / 2, height => $height, ] ], 1 => [ o => [ x => $width / 2, y => 0], size => [ width => $width / 2, height => $height, ] ]]; case 3:// | 0 | 1 |// | 2 | |return [ 0 => [ o => [ x => 0, y => 0 ], size => [ width => $width / 2, height => $height / 2, ] ], 1 => [ o => [ x => $width / 2, y => 0], size => [ width => $width / 2, height => $height, ] ], 2 => [ o => [ x => 0, y => $height / 2 ], size => [ width => $width / 2, height => $height / 2, ] ],]; default:// >= 4:// | 0 | 1 |// | 2 | 3 |return [ 0 => [ o => [ x => 0, y => 0 ], size => [ width => $width / 2, height => $height / 2, ] ], 1 => [ o => [ x => $width / 2, y => 0], size => [ width => $width / 2, height => $height / 2, ] ], 2 => [ o => [ x => 0, y => $height / 2 ], size => [ width => $width / 2, height => $height / 2, ] ], 3 => [ o => [ 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");
效果立马出来了:
赞一个~
(详细代码见 /clarence-pan/thumbnail)