Base64 encoding is a method of encoding binary data into text. In web development, base64 encoding is often used to inline images into HTML to reduce the number of server requests required to render a page. The screenshot below shows the result of a speed test on my about page.
As you can see, the entire page loads with a single server request even though there are two social media icons on the page. This is because the Twitter and Instagram PNGs have been base64 encoded straight into my header template.
Which Images Should be base64 Encoded?
Base64 encoding images into HTML can help reduce server requests, but it definitely comes at a cost. Since base64 encoded images are inlined straight into HTML, they cannot be stored in the browser cache to fulfill subsequent requests. In general, I would only recommend base64 encoding extremely small images. In practice, this would mainly apply to social media icons in PNG format, which can often be heavily compressed due to their single-color nature. For reference, my 64x64 px Twitter icon weighs in at 1,019 bytes.
You should never base64 encode large images unless you have a specific reason to do so. Since base64 encoded images are approximately 30% larger than their unencoded counterparts, encoding large images can add unnecessary bloat to your page size. Choosing whether to base64 encode an image is always a game of compromises. Giving up browser caching and inlining a base64 encoded image can result in a speed boost, or it may not. Thus, you should always do a comprehensive before and after comparison when experimenting with base64 encoded images. On staging-brian.kinsta.cloud, I base64 encoded my social icons for three reasons.
- My home page is almost always image-free, and I liked the idea of loading the entire page with one server request. Honestly, it’s a vanity metric.
- After doing some before and after comparisons, I found that base64 encoding the icons did in fact result in faster load times.
- I use Cloudflare’s “cache everything” feature, which allows me to cache my pages on Cloudflare’s extensive network of edge servers. Serving a single request from Cloudflare is extremely fast, and this is why most of my pages fully load in under 200 ms.
How to base64 Encode Images
Base64 encoding images is super easy. The first thing you should do is optimize the image for base64 encoding. I highly recommend TinyPNG for optimization of PNG files. If you’re base64 encoding other image formats, there are other format-specific optimization tools as well. Next, use a base64 encoder like this one to encode the image. When the image encoding is finished, you’ll see a string of text like the one below.
The long string of seemingly random letters and numbers is a text-based representation of the uploaded image. In this case, the string of text above is the base64 encoded version of my favicon. Finally, here’s how you can inline a base64 encoded image in your theme’s template.
When used correctly, base64 encoding is a powerful tool that can be used to reduce server requests and increase the performance of your WordPress site. To recap, only use base64 encoding for extremely small images, and always be sure to run a before and after speed test to ensure base64 encoding images contributes positively to your site’s performance.