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.
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
fl_lossy in URLs). For example, this animated GIF named
kitten_fighting, already uploaded to Cloudinary, is 6.3 MB in size.
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:
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:
- Crop the GIF to a width of 50 percent and a height of 80 percent.
- Add an uploaded PNG image named
cloudinary_iconas 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.
- Apply lossy compression with a quantity value of 50 percent.
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:
- To generate much smaller images in which to embed videos, have Cloudinary [automatically convert GIFs to videos] (https://cloudinary.com/blog/reduce_size_of_animated_gifs_automatically_convert_to_webm_and_mp4).
- Likewise, to enable users to upload videos but display them as animated GIFs, have Cloudinary dynamically convert videos to GIFs. Also, as a display enhancement, consider creating a single animated GIF from multiple images, whereby each image is shown as a single frame of the single GIF.
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.
- Image Formats: Getting it Right
- Progressive JPEGs and green Martians
- Animated WebP – how to convert animated GIF to WebP and save up to 90% bandwidth
- JPEG Image Optimization Without Compromising Quality With JPEGmini and Cloudinary
- Check for WebP Browser Support to Dynamically Deliver Images
- Adopting the WebP Image Format for Android on Websites Or Native Apps
- Optimizing Animated GIFs With Lossy Compression