Let me show you the renderings first. If you are still satisfied, please continue reading:
The imagick mentioned here is an extension of ImageMagick under PHP. Installing it with pecl is easy and simple with just one command:
Copy the code The code is as follows:
sudo pecl install imagick
(you still need to add it in php.ini after the extension is installed) Go to extension=imagick.so, and then remember to restart the apache or php-fpm service.)
Recently, there is a need to combine multiple pictures to generate thumbnails, and I just want to use this powerful imagick extension.
This requirement is to generate thumbnails like this:
1. If there is 1 picture, directly generate a thumbnail of this picture;
2. If there are 2 pictures, one on the left and one on the Right side, half each;
3. If there are 3 pictures, the left sides of the two will be equally distributed, and one will occupy the right side;
4. If there are 4 pictures, the space will be evenly distributed like a field;
5. If there are more pictures, only take the first 4 pictures and generate thumbnails according to the grid pattern.
There are quite a few rules, but it’s not too complicated. I figured it out quickly:
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, ] ], ]; } } }
Try it out:
Copy the code The code is as follows:
$thumbnail = clarencethumbnailThumbnail::createFromImages($srcImages, 240, 320);
$thumbnail->writeImage($outputDir."/example.jpg");
The above content introduces how PHP uses imagick to generate combinations I hope this helps everyone with the relevant knowledge about thumbnails!
The above introduces the use of imagick in PHP to generate combined thumbnails, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.