Skip to content

Optimizing Animated GIFs With Lossy Compression

Even though the image format animated GIFs are gaining popularity, their file size is usually large, causing slow loading and incurring high bandwidth costs. Besides, the GIF format is old and not optimized for modern video clips. The developer’s job of effecting fast loading of animated GIFs and delivering optimized images is complex and time-consuming.

With Cloudinary, you can convert animated GIFs to videos, reducing the file size and saving bandwidth (see this post for more details). However, videos are much harder to embed in websites and apps than regular images, let alone that auto-playing of videos does not work in some browsers and mobile devices. Alternatively, you can convert animated GIFs to animated WebP files. Unfortunately, the WebP format, which, being developed by Google, works only on Chrome, Android, and Opera, but not on most of the other popular mobile devices and browsers.

Furthermore, GIF conversion tools tend to produce either large, high visual-quality files or small, low visual-quality ones, neither of which lives up to optimization standards.

When lossy compression starts, filtering occurs, eliminating certain pixel data through an increase of redundant patterns along scan lines. In reality, the term lossy compression is a misnomer for GIFs, whose format is palette based, because the related compression algorithms are lossless, hence no data loss in the output.

Note:

Because of the 8-bit GIF limit of 256 colors, converting other image formats to GIF does result in data loss.

As reference, read this section in the Cloudinary documentation on image optimization: the whys, the whats, and the tools.

Optimizing animated GIFs with a lossy-compression technique yields smaller yet visually appealing images, a win-win. With Cloudinary, you apply lossy compression through the platform’s on-the-fly, dynamic URLs with no need for any software or additional computational power. Why? Because the compression process takes place in the cloud.

All you need to do is set the flag parameter to lossy (fl_lossy in URLs). For example, this animated GIF named kitten_fighting, already uploaded to Cloudinary, is 6.3 MB in size. Loading code examples original non-optimized animated GIF

Adding the fl_lossy parameter to the delivery URL reduces that size by 40 percent to 2.5 MB. The optimized GIF, as shown below, looks just as sharp as the original. Loading code examples Optimized animated GIF with lossy compression

An excellent tip on GIF compression: to fine-tune the level of lossy compression in compressed animated GIFs, add the quality parameter (q in URLs), whose default value is 80. Enabling lossy compression and setting the quality parameter to 50 for the kitten GIF produces a 2.1-MB image, approximately 30 percent of the original size:

Loading code examples 50-percent quality lossy animated GIF

You can apply lossy compression on Cloudinary with any of the platform’s rich set of image optimization and transformation capabilities to match any graphic design, dimension, device, browser, responsive layout, and such. Lossy compression can also optimize generated images. So, instead of optimizing the original, large animated GIF, you can optimize a transformed or cropped version for display.

For example, these steps generate and deliver a calibrated version of the kitten GIF:

  1. Crop the GIF to a width of 50 percent and a height of 80 percent.
  2. Add an uploaded PNG image named cloudinary_icon as an overlay. Resize the overlay to a width of 40 pixels, positioned 5 pixels from the top-right corner of the animated GIF; and make the overlay 40 percent semitransparent.
  3. Apply lossy compression with a quantity value of 50 percent.

Voila! The optimized GIF size is only 765 KB, 60 percent less than the original size of 1.9 MB. Loading code examples Animated GIF resized, with overlay added and 50-percent lossy GIF compression

To recap, applying lossy compression to animated GIFs yields two major benefits: the flexibility of displaying those lively GIFs and the bandwidth savings from smaller yet still-visually-pleasing images, not to mention an enhanced user experience. The setup takes only minimal effort on your part, freeing you up to focus on developing websites and apps.

Keep in mind these two handy tips:

Lossy compression for animated GIFs is available in all of Cloudinary’s free and paid plans. To give it a try, sign up for a free account and forge ahead. You’ll likely be impressed.

Back to top

Featured Post