How to Improve Your Site's Performance When Using GIFs
The GIF (Graphics Interchange Format) format was originally developed in 1987. Debuted by Steve Wilhite of Compuserve, GIFs improved on the black-and-white images in use during that time by allowing the use of 256 colors while maintaining a compressed format that could still be loaded by those utilizing slow modems. Furthermore, web developers and designers could create animations via timed delays. And to this day, little has changed regarding GIFs.
Due to its simplicity, the widespread support for this format, and the ease with which it can be used to stream video clips, the GIF format is the oldest file format still commonly used today. This frame animation feature of GIFs ensures that the format remains popular, despite the rise of JPEG and PNG images.
In spite of their popularity and ubiquitousness on the Internet (especially with regards to animated GIFs), GIFs are not the most performant of image options. If you are using GIFs on your sites, it’s important that you take care to optimize your GIFs so that they do not create too much overhead.
This article will cover ways to optimize your GIFs, both static and animated, and will offer an excellent alternative you can use to eliminate the page bloat resulting from use of GIFs as animation.
Why should you optimize your GIFs?
Performance matters when it comes to designing your web pages, and GIFs are not the most performant of image options. While they are excellent for capturing your user’s attention and are universally liked for providing short bursts of information in an entertaining way, GIFs were not designed for animation (despite them being commonly used for such). As such, usage of GIFs leads to heavy page weights and poor user experiences resulting from slow page load speeds.
How to improve the performance of your site while using GIFs
In this section, we’ll cover several ways you can improve the performance of your site with regards to using GIFs. We’ll first dig into ways to handle static GIFs, and we’ll end by discussing ways to minimize the overhead resulting from animated GIFs.
There are two methods for compressing images:
One of the primary methods for optimizing GIFs is to compress them. There are two methods of compression that are commonly used:
- Lossy compression: Lossy compression removes some of the data from the original file, resulting in an image with a reduced file size. However, every time you save the file after compression, the quality of the graphic degrades somewhat, which can result in a fuzzy, pixelated image over time.
- Lossless compression: Lossless compression preserves all of the data from the original file, which means that the compressed file can be uncompressed to gain the original file. While your file size remains larger than if you had used lossy compression, your image’s quality does not degrade over time.
Later on in this post, we’ll cover the impact of both types of optimization on GIFs.
Improve the performance of sites that are using static GIFs by converting to PNG.
The easiest way to improve the performance of your site is to render your image using the PNG format instead of the GIF format. While the two formats are very similar in terms of being good choices for displaying simple graphics, PNG files have the advantage of being able to compress to a size 5–25% smaller than the equivalent GIF file. GIFs were originally created to use a lossless compression technique called the Lempel-Ziv-Welch (LZW) algorithm, which was defined in the 1970s. However, modern compression techniques are much more performant than LZW, and you can take advantage of this by using formats that utilize these techniques, such as PNGs.
Improve the performance of sites that are using animated GIFs one of two ways:
Animated GIFs, while extremely popular, can be huge files that require lengthy load times. For example, a GIF that is just a few seconds long can be a few megabytes in size. To improve the performance of your site, use one of the following techniques:
- Lossy optimization
- Converting your animated GIF to a HTML5 video
Lossy optimization on animated GIFs
Because the vast majority of data comprising animated GIFs is graphical data, and because lossless optimizations cannot modify graphical data, you have only one viable option when it comes to optimizing an animated GIF beyond the bare minimum: lossy optimization techniques.
Lossy optimizations work because the human eye does not do a very good job at distinguishing between subtle changes in color. For example, an image might contain thousands of shades of one color, with one pixel showing as only slightly different from the ones next to it. Because your eye won’t be able to differentiate between the two shades, the image file can easily be manipulated: One of the colors replaces the other, making the file smaller.
Because animated GIFs are essentially a series of individual GIFs, you can utilize these techniques to decrease the size of your animated file. By making each individual file smaller, your overall file is smaller as well. One way you can do this is by utilizing a simple software suite that can automatically perform such compressions (such as a modified version of gifsicle).
Converting animated GIFs to HTML5 videos
While you can minimize the size of an animated GIF, you may still end up with a file that is larger than it needs to be. GIFs were never intended to store video, and what is now considered animation is really the result of an attempt to reduce overhead on the storage and transmission of multiple images that share identical metadata. Today, however, we have another option that could potentially make your GIFs up to 95% smaller: converting your animated GIFs to HTML5 video.
HTML5 video is a catch-all term for a modern web browser’s ability to play video content using the <video> tag without needing to use external plugins. When this feature was first released in 2009, there was a lot of debate over how such videos would be stored and how they would be encoded. Today, though, the accepted standard is an H.264-encoded video stored in an MP4 container file (which, for simplicity’s sake, we’ll refer to as an MP4 video from here on out). In addition to looking a lot better due to its being designed to stream video, MP4 files are much smaller as well:
There are many ways to convert your animated GIF to MP4, such as the popular open-source command-line tool ffmpeg and the web-based utility Cloud Convert. Using the latter, you can see the file size savings possible by making the conversion.
Here’s the original animated GIF:
Here is the MP4 video that’s created from the GIF:
Looking at the sizes of the files, we see that the original was 100 KB. By converting the GIF to MP4, we end up with a file that is just 23 KB, which is 75% smaller:
GIFs are the oldest file format still commonly used today due to their simplicity, near-universal support, and ability to be used as animation. Despite these positive features, GIFs tend to be large files, resulting in page bloat that can negatively impact the performance of your webpages and lead to poor user experiences. As such, you should consider serious optimization of static GIFs, moving away from animated GIFs, and implementing video clips using more modern techniques such as HTML5/MP4 videos. And for additional in-depth information on implementing these changes, download Rigor’s free ebook, The Book of GIF: A Comprehensive Guide to Optimizing GIFs.