The imagick mentioned here is an extension of ImageMagick in PHP. This article describes how to use imagick to generate composite thumbnails in PHP. if you are satisfied with it, refer to this article, continue reading:
Imagick is an extension of ImageMagick in PHP. Using pecl for installation is a simple command:
The code is as follows:
Sudo pecl install imagick
(After the extension is installed, add extension = imagick. so to php. ini, and restart apache or php-fpm .)
Recently, we have a need to combine multiple images to generate thumbnails, just using this powerful imagick extension.
To generate a thumbnail like this:
1. if one image exists, a thumbnail of the image is generated directly;
2. if there are two images, one is on the left and the other is on the right;
3. if there are three images, the two images are evenly distributed on the left and the other images are exclusive to the right;
4. if there are four images, the space will be evenly allocated like a field lattice;
5. for more images, only the first four images are taken and the thumbnails are generated by field.
There were a lot of such rules, but they were not too complicated and were coming up soon:
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 one:
The code is as follows:
$ Thumbnail = \ clarence \ thumbnail \ Thumbnail: createFromImages ($ srcImages, 240,320 );
$ Thumbnail-> writeImage ($ outputDir. "/example.jpg ");
The above content introduces the knowledge of using imagick to generate composite thumbnails in PHP. I hope it will be helpful to you!