PHP下使用强大的imagick轻松生成组合缩略图,imagick缩略图_PHP教程

WBOY
Freigeben: 2016-07-12 08:58:53
Original
712 Leute haben es durchsucht

PHP下使用强大的imagick轻松生成组合缩略图,imagick缩略图

<code class="language-meta">project: blog
target: use-imagick-to-composite-images-thumbnail.md
date: 2016-02-19
status: publish
tags:
    - php
    - imagick
    - thumbnail
categories:
    - php
</code>
Nach dem Login kopieren

这里说的imagickImageMagick 在PHP下的扩展。使用pecl安装起来那叫一个轻松简单 —— 一条命令就搞定:

<code class="language-sh">sudo pecl install imagick
</code>
Nach dem Login kopieren

(扩展装好后还是要在php.ini中加上extension=imagick.so,然后记得重启apachephp-fpm服务。)

最近有个需求是要把多张图片组合起来生成缩略图,刚好用用这个强大的imagick扩展。
这个需求是要这样生成缩略图:

这规则还真不少,不过还不算太过复杂,很快搞出来了:

<code class="language-php">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, &#39;white&#39;, &#39;jpg&#39;);
        $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[&#39;to&#39;][&#39;x&#39;], $cfg[&#39;to&#39;][&#39;y&#39;]);
        }
    }

    protected function makeCompositeThumbnail(\Imagick $img, $cfg){
        $img->cropThumbnailImage($cfg[&#39;size&#39;][&#39;width&#39;], $cfg[&#39;size&#39;][&#39;height&#39;]);
        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 => [
                        &#39;to&#39; => [ &#39;x&#39; => 0, &#39;y&#39; => 0 ],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width,
                            &#39;height&#39; => $height,
                        ]
                    ]
                ];
            case 2:
                // | 0 | 1 |
                return [
                    0 => [
                        &#39;to&#39; => [ &#39;x&#39; => 0, &#39;y&#39; => 0 ],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height,
                        ]
                    ],
                    1 => [
                        &#39;to&#39; => [ &#39;x&#39; => $width / 2, &#39;y&#39; => 0],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height,
                        ]
                    ]
                ];
            case 3:
                // | 0 | 1 |
                // | 2 |   |
                return [
                    0 => [
                        &#39;to&#39; => [ &#39;x&#39; => 0, &#39;y&#39; => 0 ],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height / 2,
                        ]
                    ],
                    1 => [
                        &#39;to&#39; => [ &#39;x&#39; => $width / 2, &#39;y&#39; => 0],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height,
                        ]
                    ],
                    2 => [
                        &#39;to&#39; => [ &#39;x&#39; => 0, &#39;y&#39; => $height / 2 ],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height / 2,
                        ]
                    ],
                ];
            default:
                // >= 4:
                // | 0 | 1 |
                // | 2 | 3 |
                return [
                    0 => [
                        &#39;to&#39; => [ &#39;x&#39; => 0, &#39;y&#39; => 0 ],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height / 2,
                        ]
                    ],
                    1 => [
                        &#39;to&#39; => [ &#39;x&#39; => $width / 2, &#39;y&#39; => 0],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height / 2,
                        ]
                    ],
                    2 => [
                        &#39;to&#39; => [ &#39;x&#39; => 0, &#39;y&#39; => $height / 2 ],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height / 2,
                        ]
                    ],
                    3 => [
                        &#39;to&#39; => [ &#39;x&#39; => $width / 2, &#39;y&#39; => $height / 2],
                        &#39;size&#39; => [
                            &#39;width&#39; => $width / 2,
                            &#39;height&#39; => $height / 2,
                        ]
                    ],
                ];
        }
    }
}

</code>
Nach dem Login kopieren

用个试试:

<code class="language-php">$thumbnail = \clarence\thumbnail\Thumbnail::createFromImages($srcImages, 240, 320);
$thumbnail->writeImage($outputDir."/example.jpg");

</code>
Nach dem Login kopieren

效果立马出来了:

赞一个~

(详细代码见 http://github.com/clarence-pan/thumbnail)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1100705.htmlTechArticlePHP下使用强大的imagick轻松生成组合缩略图,imagick缩略图 project: blogtarget: use-imagick-to-composite-images-thumbnail.mddate: 2016-02-19status: publishtags: - p...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage