麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > PHP > 正文

PHP利用imagick生成組合縮略圖

2024-05-04 23:43:09
字體:
來源:轉載
供稿:網友
這里說的imagick 是 ImageMagick 在PHP下的擴展,本文給大家介紹PHP利用imagick生成組合縮略圖,需要的朋友參考下
 

先給大家炫下效果圖,如果大家覺得還很滿意,請繼續往下閱讀:

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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本黄色免费片 | 美国一级免费视频 | 亚洲精品在线观看免费 | 免费毛片视频 | 宅男噜噜噜66国产在线观看 | 成年人黄色片视频 | 999精品国产 | 亚洲一区在线免费视频 | 国产羞羞视频在线观看免费应用 | 极品销魂一区二区三区 | av在线播放免费观看 | 蜜桃传媒视频麻豆第一区免费观看 | 日韩欧美激情视频 | 午夜视频色 | 欧美一区2区三区4区公司二百 | 久久精品a一级国产免视看成人 | 最新在线中文字幕 | 国产91一区 | 欧美淫视频 | 九九热视频这里只有精品 | 看片一区| h网站在线观看 | 国产1区2区3区在线观看 | 成人免费福利网站 | 免费国产wwwwwww网站 | 欧美日韩视频网站 | 午夜在线成人 | 国产一级aa大片毛片 | 成人午夜在线免费视频 | 91 在线免费观看 | 久久99精品久久久久久秒播放器 | 爱操视频| 国产成人高清成人av片在线看 | 色女生影院| 成人性生活视频 | 19禁国产精品福利视频 | 91短视频在线免费观看 | 成人黄色短视频在线观看 | 成人做爰高潮片免费视频韩国 | 密室逃脱第一季免费观看完整在线 | 一区二区三区欧美在线 |