PHP利用imagick生成組合縮略圖
先給大家炫下效果圖,如果大家覺(jué)得還很滿(mǎn)意,請(qǐng)繼續(xù)往下閱讀:
這里說(shuō)的imagick 是 ImageMagick 在PHP下的擴(kuò)展。使用pecl安裝起來(lái)那叫一個(gè)輕松簡(jiǎn)單一條命令就搞定:
sudo pecl install imagick
(擴(kuò)展裝好后還是要在php.ini中加上extension=imagick.so,然后記得重啟apache或php-fpm服務(wù)。)
最近有個(gè)需求是要把多張圖片組合起來(lái)生成縮略圖,剛好用用這個(gè)強(qiáng)大的imagick擴(kuò)展。
這個(gè)需求是要這樣生成縮略圖:
1.如果有1張圖片,就直接生成這張圖片的縮略圖;
2.如果有2張圖片,則一張?jiān)谧筮呉粡堅(jiān)谟疫?,各一半?br />
3.如果有3張圖片,則兩張左邊平均分配,一張獨(dú)占右邊;
4.如果有4張圖片,則像田字格一樣平均分配空間;
5.更多張圖片,則只取前4張,按田字格方式生成縮略圖。
這規(guī)則還真不少,不過(guò)還不算太過(guò)復(fù)雜,很快搞出來(lái)了:
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, ] ], ]; } } }
用個(gè)試試:
$thumbnail = \clarence\thumbnail\Thumbnail::createFromImages($srcImages, 240, 320);
$thumbnail->writeImage($outputDir."/example.jpg");
以上內(nèi)容給大家介紹了PHP利用imagick生成組合縮略圖的相關(guān)知識(shí),希望對(duì)大家有所幫助!
相關(guān)文章
web server使用php生成web頁(yè)面的三種方法總結(jié)
web server使用php生成web頁(yè)面的三種方法。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10IOS蘋(píng)果AppStore內(nèi)購(gòu)付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp)
這篇文章主要介紹了IOS蘋(píng)果AppStore內(nèi)購(gòu)付款的服務(wù)器端php驗(yàn)證方法(使用thinkphp),需要的朋友可以參考下2022-12-12thinkPHP基于ajax實(shí)現(xiàn)的菜單與分頁(yè)示例
這篇文章主要介紹了thinkPHP基于ajax實(shí)現(xiàn)的菜單與分頁(yè),結(jié)合實(shí)例形式分析了thinkPHP基于ajax無(wú)刷新交互實(shí)現(xiàn)菜單及分頁(yè)功能的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-07-07php+redis實(shí)現(xiàn)多臺(tái)服務(wù)器內(nèi)網(wǎng)存儲(chǔ)session并讀取示例
這篇文章主要介紹了php+redis實(shí)現(xiàn)多臺(tái)服務(wù)器內(nèi)網(wǎng)存儲(chǔ)session并讀取示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01PHP YII框架開(kāi)發(fā)小技巧之模型(models)中rules自定義驗(yàn)證規(guī)則
yii的models中rules部分是一些表單的驗(yàn)證規(guī)則,對(duì)于表單驗(yàn)證有幫助,在相應(yīng)的視圖(views)里面添加了表單,在表單被提交之前程序都會(huì)自動(dòng)先來(lái)這里面的規(guī)則里驗(yàn)證,只有通過(guò)對(duì)其有效的限制規(guī)則后才能被提交,可以很有效地保證表單安全和信息的有效性2015-11-11在Mac OS上自行編譯安裝Apache服務(wù)器和PHP解釋器
這篇文章主要介紹了在Mac OS上編譯安裝Apache服務(wù)器和PHP解釋器的教程,盡管Mac上自帶Apache和PHP,但由于版本或者其他原因很多情況下還是自己配置更為舒心,需要的朋友可以參考下2015-12-12php中static 靜態(tài)變量和普通變量的區(qū)別
靜態(tài)變量與普通變量的區(qū)別在哪里呢,對(duì)于許多初學(xué)php的朋友來(lái)講可能不是點(diǎn)擊的明白了,今天我們來(lái)看看php中static 靜態(tài)變量和普通變量的區(qū)別吧,需要的朋友可以參考下2016-12-12PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法
下面小編就為大家?guī)?lái)一篇PHP調(diào)用OpenOffice實(shí)現(xiàn)word轉(zhuǎn)PDF的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11