Home >Backend Development >PHP Tutorial >Use the powerful imagick to easily generate combined thumbnails under PHP, imagick thumbnails_PHP tutorial

Use the powerful imagick to easily generate combined thumbnails under PHP, imagick thumbnails_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:58:53732browse

Use the powerful imagick under PHP to easily generate combined thumbnails, imagick thumbnails

<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>

The imagick mentioned here is the extension of ImageMagick under PHP. Installation using pecl is easy and simple - just one command:

<code class="language-sh">sudo pecl install imagick
</code>

(After the extension is installed, you still need to add extension=imagick.so to php.ini, and then remember to restart the apache or php-fpm service.)

Recently, there was a need to combine multiple pictures to generate thumbnails, and I happened to use this powerful imagick extension.
This requirement is to generate thumbnails like this:

There are quite a few rules, but they are not too complicated and I figured them out quickly:

<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>

Try it out:

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

</code>

The effect came out immediately:

Like it~

(See http://github.com/clarence-pan/thumbnail for detailed code)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1100705.htmlTechArticleUse the powerful imagick to easily generate combined thumbnails under PHP, imagick thumbnail project: blogtarget: use-imagick-to -composite-images-thumbnail.mddate: 2016-02-19status: publishtags: - p...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn