Php code that perfectly implements GIF animation thumbnails

Source: Internet
Author: User

The following describes the problem through 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:

Option 1: Use the Imagick module in PHP:

Copy codeThe Code is as follows: <? Php
$ Image = new Imagick('old.gif ');
$ I = 0;
Foreach ($ image as $ frame ){
$ Frame-> writeImage ('old _ '. $ I ++. '.gif ');
}
?>

Option 2: Use the convert command provided by ImageMagick:Copy codeThe Code is as follows: shell> convert old.gif old_+d.gif

The following figure shows the GIF animation frames:

GIF animation Frames

Obviously, in order to compress the GIF animation, the first frame is used as the template, and the remaining frames are accumulated in sequence according to the appropriate offset, and only different pixels are retained, as a result, the size of each frame is different, which causes obstacles to thumbnails.

Let's take a look at how to use the Imagick module in PHP to perfectly implement GIF animation thumbnails:

Copy codeThe Code is as follows: <? Php
$ 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 most important thing in the Code is the coalesceimages method, which ensures that the size of each frame is consistent. in the manual, it is:

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 each subsequent image varies in size and offset. returns a new Imagick object where each image in the sequence is the same size as the first and composited with the next image in the sequence.

At the same time, pay attention to the optimizeImageLayers method, which deletes duplicate pixel content. In the manual, it is:

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

BTW: You can use the quantizeImages method to further compress a bit more perfectly.

Note: Both coalesceimages and optimizeImageLayers return New Imagick objects!

If you are more comfortable with shell operations, you can implement GIF animation thumbnails as follows:

Copy codeThe Code is as follows: shell> convert old.gif-coalesce-thumbnail 50x50-layers optimize new.gif

The generated new.gif is as follows:

New.gif

The convert version is smaller than the php version, which is caused by inconsistent API implementations.

In addition, if the size of the thumbnail does not conform to the ratio of the source image, crop or fill the image to avoid deformation. As this article mainly discusses the particularity of GIF animation thumbnails, we will not continue to discuss these issues, if you are interested, do it yourself.

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.