PHP uses Imagick to generate combined thumbnail images

Source: Internet
Author: User
First of all to dazzle, if you feel very satisfied, please continue to read:

The Imagick here is the extension of ImageMagick under PHP. Use PECL to install it. Call it a simple, easy command:

Copy the Code code as follows:


sudo pecl install Imagick

(add extension=imagick.so to the php.ini after the extension is installed, and then remember to restart the Apache or PHP-FPM service.) )

A recent requirement is to combine multiple images to create thumbnails, just using this powerful imagick extension.

This requirement is to generate thumbnails like this:

1. If there are 1 pictures, the thumbnail image of this image is generated directly;

2. If there are 2 pictures, one on the left one on the right, the other half;

3. If there are 3 pictures, then the two left are evenly distributed, one exclusive to the right;

4. If there are 4 pictures, the average space is allocated like the field grid;

5. More pictures, then only take the first 4, according to the field of the grid to generate thumbnails.

It's a lot of rules, but it's not too complicated, and it's going to work out quickly:

namespace \clarence\thumbnail;class thumbnail extends \imagick{/*** @param array $images * @param int $width * @param int $h eight* @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 ' and ' = ' $heigh t/2,]],1 = [' to ' + = [' x ' = = $width/2, ' y ' = + 0], ' size ' = = [' width ' = ' = ' + $width/2, ' height ' = $h eight,]],2 = [' to ' + = [' x ' = = 0, ' y ' = + $height/2], ' size ' = = [' width ' = ' + ' $width/2, ' height ' = $height/2,]],];d efault://>= 4://| 0 | 1 |//| 2 | 3 |return [0] = [' to ' + = ' x ' = 0, ' y ' + 0], ' size ' = = ' width ' = $width/2, ' height ' and ' = ' $height /2,]],1 = [' to ' + = [' x ' = + $width/2, ' y ' = + 0], ' size ' = = [' width ' = ' = ' + $width/2, ' height ' = $hei ght/2,]],2 = [' to ' + = [' x ' = 0, ' y ' = + $height/2], ' size ' = = [' width ' = ' + $width/2, ' height ' =&gt ; $height/2,]],3 = [' to ' + = [' x ' = = $width/2, ' y ' = + $height/2], ' size ' = = [' width ' = = $width/2, ' Height ' = $height/2,]],];}}

Try it with a test:

Copy the Code code as follows:


$thumbnail = \clarence\thumbnail\thumbnail::createfromimages ($srcImages, 240, 320);
$thumbnail->writeimage ($outputDir. " /example.jpg ");

The above content to introduce PHP to use Imagick to generate a combination of thumbnails of the relevant knowledge, we hope to help!

The above introduces PHP using Imagick to generate a combination of thumbnails, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.