Easily generate combined thumbnails under PHP using powerful Imagick, Imagick thumbnails _php Tutorials

Source: Internet
Author: User

Use powerful Imagick to easily generate combined thumbnails under PHP, Imagick thumbnail images


project: blogtarget: use-imagick-to-composite-images-thumbnail.mddate: 2016-02-19status: publishtags:    - php    - imagick    - thumbnailcategories:    - php

This imagick is what is said to be an ImageMagick extension under PHP. It's easy to use to pecl install it--a single command is done:

sudo pecl install imagick

(after the extension is installed, add it in the php.ini extension=imagick.so , then remember to restart 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:

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 $height * @return static * @throws thumbnailexception */public static function CreateFrom        Images ($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;        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]);            $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 ' and $height,            ]                    ]                ]; Case 2://| 0 |                1 |     return [0] = [' to ' + = ' x ' = 0, ' y ' = 0],                   ' Size ' = = [' width ' = $width/2, ' height ' = $height,]], 1 = [' To ' =& Gt [' x ' = = $width/2, ' y ' = + 0], ' size ' = = [' width ' = = $widt            H/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 ' =& Gt                       $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 ' =& Gt      $width/2, ' y ' = + 0],                  ' Size ' = = [' width ' = $width/2, ' height '  = = $height/2,]], 2 = = [' To ' = = [' x ' = 0, ' y ' = + $height/2], ' size ' = = [' Width ' =& Gt                    $width/2, ' height ' = $height/2,],                        3 = [' to ' + = [' x ' = ' = $width/2, ' y ' = + $height/2], ' Size ' = = [' width ' = $width/2, ' height ' and ' = ' $height        /2,]],]; }    }}

Try it with a test:

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

The effect came out immediately:

Praise A ~

(see Http://github.com/clarence-pan/thumbnail for detailed code)

http://www.bkjia.com/PHPjc/1100705.html www.bkjia.com true http://www.bkjia.com/PHPjc/1100705.html techarticle use powerful Imagick to easily generate combined thumbnails under PHP, Imagick thumbnail project:blogtarget:use-imagick-to-composite-images-thumbnail.mddate: 2016-02-19status:publishtags:-P ...

  • 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.