程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP下使用強大的imagick輕松生成組合縮略圖,imagick縮略圖

PHP下使用強大的imagick輕松生成組合縮略圖,imagick縮略圖

編輯:關於PHP編程

PHP下使用強大的imagick輕松生成組合縮略圖,imagick縮略圖


project: blog
target: use-imagick-to-composite-images-thumbnail.md
date: 2016-02-19
status: publish
tags:
    - php
    - imagick
    - thumbnail
categories:
    - php

這裡說的imagickImageMagick 在PHP下的擴展。使用pecl安裝起來那叫一個輕松簡單 —— 一條命令就搞定:

sudo pecl install imagick

(擴展裝好後還是要在php.ini中加上extension=imagick.so,然後記得重啟apachephp-fpm服務。)

最近有個需求是要把多張圖片組合起來生成縮略圖,剛好用用這個強大的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['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");

效果立馬出來了:

贊一個~

(詳細代碼見 http://github.com/clarence-pan/thumbnail)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved