Core HTML

Lesson 32: Base64 Images

Overview

In this HTML lesson, we demonstrate how to encode images in Base64 and embed them into an image element in an HTML document. Ordinary image elements use a file on the local file system or a file at a remote URL as the source for image data. Base64 encoding allows us to include the data for an image directly in an image element so that the image does not rely on an external resource, like a file that is local or at a remote URL. Base64 uses one of 64 characters to represent 6 bits of data or 64 possible values. A conversion program for creating base64 encodings is available on our lesson page.

Ordinary Images

With an ordinary image element, we set the src attribute value to the name of a file on the local file system or at a remote URL. This example uses a local image file of a small red "X" in a 9 by 9 pixel image. The image is deliberately small to make the base64 encoding easier to explain.

Example1.html

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net HTML</title>
  </head>
  <body>
    <img src="SmallX_9x9.png">
  </body>
</html>
 

Output

 

Base64 Encoding

We can replace the source file of an image with its base64 encoding, as we do in this example. This HTML document looks exactly the same as the previous example. The only difference is that, instead of loading an external file, we have included all of the image data inside the src attribute. This long base64 string contains all of the data of the image file. We kept the image file small so that the entire base64 encoding could be seen easily. A larger image would take up several pages of text.

Example2.html

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net HTML</title>
  </head>
  <body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJCAYAAADgkQYQAAAALHRFWHRDcmVhdGlvbiBUaW1lAFN1biAyOSBOb3YgMjAyMCAwMDowNDo1NiAtMDYwMG8XiDEAAAAHdElNRQfkCx0GBxWys0P5AAAACXBIWXMAAC4jAAAuIwF4pT92AAAABGdBTUEAALGPC/xhBQAAAC9JREFUeNpj+M/A8B+EGbAAuByMga4QQxxdAJdGFAl8TmAgSQFBq4hyNFaNxIQTAFMqWacR/ceiAAAAAElFTkSuQmCC">
  </body>
</html>
 

Output

 

Number Bases

To understand base64, it helps to look at some other number systems with other bases. The number system that we commonly use is called decimal, and it encodes digits with ten possible values: 0 through 9. The fundamental number system of a computer is binary, or base 2, and it encodes all digits with either a zero or a one. Octal, or base 8, is another number system that uses the digits zero through seven to encode eight possible values per digit. Hexadecimal, or base 16 numbers, are very commonly used in computer prgramming and use the numbers zero through nine and the letters "A" through "F" to represent sixteen possible values per digit: 0 through 15, respectively. Finally, Base64 uses the uppercase letters, lowercase letters, digits zero through nine, and "+" and "/" to represent 64 possible values per digit.

 
 

Base64 Conversion Program

To help you generate your own base64 image data, we have included this base64 conversion program. Just copy and paste this HTML and JavaScript into a text editor and save the file with a .html extension. Then open the document with your browser. Click the "Choose File" button and select the image file to encode. After you click the "Open" button, the image will be displayed underneath the "Choose File" button and the base64 encoding of the image will appear as a string of text beneath the image. To use the base64 encoding, select the entire string under the image and copy it into the src attribute for your image.

Base64Converter.html

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net HTML</title>
    <script type='text/javascript'>
      function EncodeImageFileAsBase64() {
        var qaSelectedFiles = document.getElementById("idFilesToLoad").files;
        if (qaSelectedFiles.length > 0) {
          var qFileToLoad = qaSelectedFiles[0];
          var qFileReader = new FileReader();
          qFileReader.onload = function(qLoadEvent) {
            var sBase64Data = qLoadEvent.target.result; // <--- data: base64
            var qImageElement = document.createElement('img');
            qImageElement.src = sBase64Data;
            document.getElementById("idImageHolder").innerHTML = qImageElement.outerHTML;
            document.getElementById("idBase64Data").innerHTML = sBase64Data;
          }
          qFileReader.readAsDataURL(qFileToLoad);
        }
      }
    </script>
  </head>
  <body>
    <input id="idFilesToLoad" type="file" onchange="EncodeImageFileAsBase64();" />
    <div id="idImageHolder"></div>
    <div id="idBase64Data" style="width:1200px;word-wrap:break-word;"></div>
  </body>
</html>
 

Output

 

Base64 Encoding

Base64 encoded images increase the image data size by one third over the original image. This is because 8 bits are required to specify a base64 digit that can only represent 64 possible values or 6 bits of data. Be aware that image files increase in size dramatically as the image size increases. So, the base64 encoding will we very long for large images. Additionally, there might be a few "=" signs at the end of the base64 encoding. These are not base64 digits, but are used to fill up the string to the nearest multiple of four bytes.

Example3.html

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net HTML</title>
  </head>
  <body>
    <img src="data:image/png;base64,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