国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

PHP利用imagick生成組合縮略圖

2024-05-04 23:43:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
這里說(shuō)的imagick 是 ImageMagick 在PHP下的擴(kuò)展,本文給大家介紹PHP利用imagick生成組合縮略圖,需要的朋友參考下
 

先給大家炫下效果圖,如果大家覺(jué)得還很滿意,請(qǐng)繼續(xù)往下閱讀:

PHP利用imagick生成組合縮略圖

這里說(shuō)的imagick 是 ImageMagick 在PHP下的擴(kuò)展。使用pecl安裝起來(lái)那叫一個(gè)輕松簡(jiǎn)單一條命令就搞定:

 

復(fù)制代碼代碼如下:

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)谟疫叄饕话耄?/p>

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è)試試:

 

復(fù)制代碼代碼如下:

$thumbnail = /clarence/thumbnail/Thumbnail::createFromImages($srcImages, 240, 320);
$thumbnail->writeImage($outputDir."/example.jpg");

 

以上內(nèi)容給大家介紹了PHP利用imagick生成組合縮略圖的相關(guān)知識(shí),希望對(duì)大家有所幫助!



注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 建阳市| 陵川县| 婺源县| 宣武区| 来宾市| 桑植县| 平度市| 邮箱| 古田县| 宁化县| 饶阳县| 恩施市| 榕江县| 大同县| 芜湖市| 星座| 河曲县| 东乌| 库车县| 海阳市| 尉犁县| 永福县| 遂溪县| 依兰县| 昂仁县| 绥阳县| 青冈县| 息烽县| 密云县| 隆化县| 射阳县| 贵阳市| 溆浦县| 秭归县| 永德县| 襄城县| 涞源县| 正阳县| 乐昌市| 宁化县| 溧水县|