First of all to dazzle the effect of the picture, if we feel that is satisfied, please continue to read:
The Imagick here is the extension of ImageMagick under PHP. Use PECL to install it. It's a simple, easy command.
Copy Code code as follows:
sudo pecl install Imagick
(add extension=imagick.so to the php.ini and remember to restart the Apache or PHP-FPM service after the extension is installed.) )
A recent requirement is to combine multiple images to generate thumbnails, just to use this powerful imagick extension.
The requirement is to generate thumbnails like this:
1. If there are 1 pictures, directly generate the thumbnail image of this picture;
2. If there are 2 pictures, one on the left one on the right side, each half;
3. If there are 3 pictures, then the two left average distribution, an exclusive right;
4. If there are 4 pictures, the same as the field of the average space allocation;
5. More pictures, only take the first 4, according to the field character format thumbnail.
It's a lot of rules, but it's not too complicated, it's going to come out 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)) {THR
ow new Thumbnailexception ("No images!");
} $thumbnail = new static ();
$thumbnail->newimage ($width, $height, ' white ', ' jpg ');
$thumbnail->compositeimages ($images);
return $thumbnail; The 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]); $im
g = $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 ' => $heigh T,]], 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 ' => $heigh T/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 ' => $heigh T/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 ' => [' W
Idth ' => $width/2, ' height ' => $height/2,]],]; }
}
}
Try it with one:
Copy Code code as follows:
$thumbnail = \clarence\thumbnail\thumbnail::createfromimages ($srcImages, 240, 320);
$thumbnail->writeimage ($outputDir.) /example.jpg ");
The above content for you to introduce PHP using Imagick to generate a combination of thumbnails of the relevant knowledge, hope to help you!