PHP Code _php tutorial for perfect gif animation thumbnails

Source: Internet
Author: User
The following is a description of the problem by a GIF animation taken from the CS Bandit game:

Animated GIF pictures: old.gif

To make the problem clearer, we'll first restore the animation frames:

Select one: Use the Imagick module in PHP:
Copy the Code code as follows:
$image = new Imagick (' old.gif ');
$i = 0;
foreach ($image as $frame) {
$frame->writeimage (' old_ '. $i + +. '. gif ');
}
?>

Option two: The convert command provided with ImageMagick:
Copy the Code code as follows:
Shell> Convert Old.gif old_%d.gif

The result is that the animated GIF frames are as follows:

GIF animation frames

It can be seen that GIF animation, in order to compress, will be the first frame as a template, the rest of the frames in accordance with the appropriate offsets in succession, and only keep different pixels, the result is that each frame size is not the same, the thumbnail is caused by obstacles.

Here's a look at how to use the Imagick module in PHP to perfectly implement GIF animation thumbnails:
Copy the Code code as follows:
$image = new Imagick (' old.gif ');
$image = $image->coalesceimages ();
foreach ($image as $frame) {
$frame->thumbnailimage (50, 50);
}
$image = $image->optimizeimagelayers ();
$image->writeimages (' New.gif ', true);
?>

The key in the code is the Coalesceimages method, which ensures that each frame is of the same size, in the words of the manual:

Composites a set of images while respecting any page offsets and disposal methods. GIF, MIFF, and MNG animation sequences typically start with an image background and all subsequent image varies in size a nd offset. Returns a new Imagick object where each image in the sequence are the same size as the first and composited with the next I Mage in the sequence.

Also pay attention to the Optimizeimagelayers method, which removes duplicate pixel content, in the words of the manual:

Compares each image the GIF disposed forms of the previous image in the sequence. From this it attempts to select the smallest cropped image to replace each frame while preserving the results of the Anim ation.

BTW: If the requirements are more perfect, you can use the Quantizeimages method to further compress.

Note: Both Coalesceimages and Optimizeimagelayers return new Imagick Objects!

If you are more accustomed to operating the shell, then you can implement the GIF animation thumbnail:
Copy the Code code as follows:
Shell> convert old.gif-coalesce-thumbnail 50x50-layers optimize new.gif

The resulting new.gif are as follows:

New.gif

There is a detail problem: The convert version will be smaller than the PHP version, which is caused by inconsistent API implementations.

In addition, if the thumbnail size does not conform to the original ratio, in order to avoid deformation, but also to consider the crop or filler, because this article mainly discusses the characteristics of GIF animation thumbnails, will not continue to discuss these issues, interested in their own to fix it.

http://www.bkjia.com/PHPjc/322716.html www.bkjia.com true http://www.bkjia.com/PHPjc/322716.html techarticle The following is a description of the problem by a GIF animation taken from the CS Bandit game: GIF animated Image: Old.gif to make the problem clearer, we first restore the animation frames: Select one: Use PHP i ...

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