Using base64 encoded data URI images

Filed under: technology, web development

comments (2) Views: 5,597

While working on a recent open source project that I released called CFDirectoryLister I had a choice to make. The goal was to have something that could be dropped into a directory and it would just work. The problem was that I wanted an attractive solution for developers, but I didn't want a lot of dependencies like icons, or external stylesheets.

The solution was to use data URI schemes. Rather than a traditional URL that points to a file on a filesystem somewhere, you provide a base64 encoded version of your file, prepended with a command indicating the mime type of the following data, and it's encoding type. The basics look something like this:


<img src="data:image/jpeg;base64,<encoded-characters> />

The goal of this approach isn't to save bandwidth so much as it is to reduce HTTP requests. In some cases converting an image to base64 actually increases the total size. You can use a base64 encoded image anywhere you'd use a normal image, including in your CSS. It's actually fairly simply to one-off encode images using Terminal for your Mac. I've included a few commands which can be issued from Terminal, along with the resulting output. The | pbcopy tacked on to the end of each command causes the output of the encoding to be copied to the Mac's clipboard. Each of these options also allow the results to be written to a file if you prefer to go that route.


python -c 'print open("me.png", "rb").read().encode("base64").replace("
","")' | pbcopy


openssl base64 < me.png | tr -d '
' | pbcopy


base64 -i me.png |  pbcopy

Of course encoding from the command line isn't always the best solution. Every modern programming language offers their own encoding options and ColdFusion is no different. Here's a few options for base64 encoding with ColdFusion:


<cfimage name="myPic" action="read" source="/path/to/picture.jpg">
<img src="data:image/*;base64,#toBase64( myPic )#" />

<cfset myPic = fileReadBinary(expandPath( "/path/to/picture.jpg" ))>
<img src="data:image/*;base64,#toBase64( myPic )#" />

Of course you're not going to want to encode those every time so if this is your approach, do it once then store it somewhere first. This might also be a good option for emails too. It might help avoid having to load in images, which means your users might see the images rather than be asked to load them. Your mileage may vary so make sure to test before sending large mailings. Good luk and have fun.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

comments powered by Disqus
coach outlet online jordan 13 grey toe beats by dre cyber monday michael kors black friday beats by dre cyber monday jordan 6 black infrared north face cyber monday michael kors cyber monday north face black friday coach outlet black infrared 23 13s north face cyber monday jordan 6 black infrared north face black friday coach cyber monday jordan 11 legend blue north face cyber monday black infrared 6s lebron 12 north face black friday jordan 11 legend blue louis vuitton outlet jordan 13 grey toe grey toe 13s beats by dre black friday coach black friday jordan 13 grey toe coach cyber monday uggs black friday jordan 13 black infrared 23 uggs cyber monday barons 13s uggs black friday beats by dre cyber monday black infrared 6s jordan 13 bred jordan 13 black infrared 23 north face black friday black infrared 6s jordan 11 legend blue michael kors black friday jordan 13 grey toe coach black friday michael kors black friday michael kors cyber monday beats by dre cyber Monday north face cyber monday coach black friday michael kors cyber monday beats by dre cyber Monday north face black friday beats by dre black friday lululemon black friday uggs black friday jordan 13 bred coach cyber monday beats by dre black friday uggs black friday coach black friday black infrared 6s