Home >Backend Development >PHP Tutorial >Use the powerful imagick to easily generate combined thumbnails under PHP, imagick thumbnails_PHP tutorial
<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, '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, ] ], ]; } } } </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)