PHP uses imagick to generate composite thumbnails and imagick thumbnails.
First, let's show off. If you still feel satisfied, please continue reading:
Imagick is an extension of ImageMagick in PHP. Using pecl for installation is a simple command:
Copy codeThe 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:
Copy codeThe 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!
Articles you may be interested in:
- PHP uses imagick to read PDF to generate png thumbnails.
- Php uses the imagick module to scale, crop, and compress images.
- The method of JPG synthesis of GIF Image Based on php_imagick_st-Q8.dll in PHP
- Use Imagick in PHP to read pdf files and generate png thumbnail instances
- How to Use imagick to generate a PSD file thumbnail in PHP
- PHP uses Imagick to crop/generate thumbnails/Add watermarks to automatically detect and process GIF images