How to display Base64 images in HTML

1,775,188

Solution 1

My suspect is of course the actual Base64 data. Otherwise it looks good to me. See this fiddle where a similar scheme is working. You may try specifying the character set.

<div>
  <p>Taken from wikpedia</p>
  <img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
    AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
        9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
</div>

You can try this Base64 decoder to see if your Base64 data is correct or not.

Solution 2

You need to specify the correct Content-type, Content-encoding and charset.

Like

 data:image/jpeg;charset=utf-8;base64,

according to the syntax of the data URI scheme:

 data:[<media type>][;charset=<character set>][;base64],<data>

Solution 3

If you have PHP on the back-end, you can use this code:

$image = 'http://images.itracki.com/2011/06/favicon.png';
// Read image path, convert to base64 encoding
$imageData = base64_encode(file_get_contents($image));

// Format the image SRC:  data:{mime};base64,{data};
$src = 'data: ' . mime_content_type($image) . ';base64,' . $imageData;

// Echo out a sample image
echo '<img src="' . $src . '">';

Solution 4

First convert your image to Base64 (encode to Base64). You can do it online or with a PHP script.

After converting you will get the result as

iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGl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yQR2qbrXiiwYN3ACh1FDmsdK2Eu4J6Tlo31dYVtCY88h5ELZIJJ+IRMzBHfyJINrigNkt5VsRiub9nXICdsYyVd2NcVvA3ScE5t2rb5JuEeyZnAhmLt9NK63vX1O5Pe8XaPSuGq1uTrfUgMEp9EJ+CQvr+BJ/AAKvAcCiAR+bf9CjAAluzmdX4AEIIAAAAASUVORK5CYII=

Now it's simple to use.

You have to just put it in the src of the image and define it there as it is in Base64-encoded form.

Example:

<img src="data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MEVBMTczNDg3QzA5MTFFNjk3ODM5NjQyRjE2RjA3QTkiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MEVBMTczNDk3QzA5MTFFNjk3ODM5NjQyRjE2RjA3QTkiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDowRUExNzM0NjdDMDkxMUU2OTc4Mzk2NDJGMTZGMDdBOSIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDowRUExNzM0NzdDMDkxMUU2OTc4Mzk2NDJGMTZGMDdBOSIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PjjUmssAAAGASURBVHjatJaxTsMwEIbpIzDA6FaMMPYJkDKzVYU+QFeEGPIKfYU8AETkCYI6wANkZQwIKRNDB1hA0Jrf0rk6WXZ8BvWkb4kv99vn89kDrfVexBSYgVNwDA7AN+jAK3gEd+AlGMGIBFDgFvzouK3JV/lihQTOwLtOtw9wIRG5pJn91Tbgqk9kSk7GViADrTD4HCyZ0NQnomi51sb0fUyCMQEbp2WpU67IjfNjwcYyoUDhjJVcZBjYBy40j4wXgaobWoe8Z6Y80CJBwFpunepIzt2AUgFjtXXshNXjVmMh+K+zzp/CMs0CqeuzrxSRpbOKfdCkiMTS1VBQ41uxMyQR2qbrXiiwYN3ACh1FDmsdK2Eu4J6Tlo31dYVtCY88h5ELZIJJ+IRMzBHfyJINrigNkt5VsRiub9nXICdsYyVd2NcVvA3ScE5t2rb5JuEeyZnAhmLt9NK63vX1O5Pe8XaPSuGq1uTrfUgMEp9EJ+CQvr+BJ/AAKvAcCiAR+bf9CjAAluzmdX4AEIIAAAAASUVORK5CYII=">

Solution 5

This will show an image of Base64 data:

<style>
    .logo {
        width: 290px;
        height: 63px;
        background: url(data:image/png;base64,copy-paste-Base64-data-here) no-repeat;
    }
</style>

<div class="logo"></div>
Share:
1,775,188
Christopher
Author by

Christopher

Updated on July 16, 2022

Comments

  • Christopher
    Christopher almost 2 years

    I'm having trouble displaying a Base64 image inline.

    How can I do it?

    <!DOCTYPE html>
    <html>
      <head>
        <title>Display Image</title>
      </head>
      <body>
        <img style='display:block; width:100px;height:100px;' id='base64image'
           src='data:image/jpeg;base64, LzlqLzRBQ... <!-- Base64 data -->' />
      </body>
    </html>
    
  • Christopher
    Christopher over 12 years
    thankyou for taking the time to answer my question... that js fiddle simply outputs a red dot?? is that what it is meant to do?
  • VinayC
    VinayC over 12 years
    @Christopher, yes - its a data URI for a red dot image (source: en.wikipedia.org/wiki/Data_URI_scheme). I am just pointing out that there is no apparent issue in what you are doing except perhaps actual base64 data. Use the given decoder and save decoded binary data as an jpeg file and try to open it. If it does not work then there is an issue in your base64 encoding.
  • Christopher
    Christopher over 12 years
    I believe I have discovered the problem is when the base64 file is being uploaded every "+" seems to be removed. Which intern nullifies the jpeg. I am posting the image from a device to the server with AJAX is it possible that "+" is not passed correctly throught because it is a htmlentity?
  • VinayC
    VinayC over 12 years
    @Christopher, "+" character can be problematic in post data. See this related SO question: stackoverflow.com/questions/1373414/…
  • Dan Dascalescu
    Dan Dascalescu about 9 years
    stackoverflow.com/questions/13265902/… suggests there's no header for encoding the HTTP body using base-64.
  • Dan Dascalescu
    Dan Dascalescu about 9 years
    Also, stackoverflow.com/questions/7285372/… - it's a MIME header, not an HTTP one. Can you expand on how exactly the content-encoding should be specified?
  • Benjamin Conant
    Benjamin Conant over 8 years
    Just used the HTML from the question above with some valid base64 data for a PNG and it works fine ... this is the correct answer.
  • Altimus Prime
    Altimus Prime over 7 years
    This solved the problem for me. Chrome would assume UTF-8 and show the images while Microsoft Edge would simply fail to render. Once I included the charset the images showed perfectly.
  • Ed of the Mountain
    Ed of the Mountain over 7 years
    Excellent solution to an iOS UIWebView cache problem that kept referencing old image data even if I deleted the image files. This answer helped me to avoid creating unique file names and having to clean-up afterwards. Thank you @NARGIS PARWEEN
  • Venu Madhav
    Venu Madhav over 6 years
    @VinayC am getting huge encoding code for my small circle..is there any way to reduce the code..?
  • VinayC
    VinayC over 6 years
    @VenuMadhav, Base64 encoded size is approx 1.37 times of original data. So, check your image data and how can you reduce the same (e.g. using small size image say 16x16, png encoded etc.).
  • Dr.jacky
    Dr.jacky almost 6 years
    Is it possible to use input instead of img? I want to show the value on the label or textbox, instead of an image.
  • Timo
    Timo about 4 years
    Is this an image encoded to a string? Can every image be represented as a string? The more detail an image has the longer the string?
  • Mtxz
    Mtxz over 3 years
    @Timo yes (here with "base64"), yes (I believe any data can be string encoded), and yes (the more data, the more characters).
  • Peter Mortensen
    Peter Mortensen almost 3 years
    But in the end it comes out as HTML. What is the HTML output?
  • Peter Mortensen
    Peter Mortensen almost 3 years
    I think this needs an explanation of how it answers the question. Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).
  • Peter Mortensen
    Peter Mortensen almost 3 years
    What is this? JavaScript? TypeScript? In what context does this code run?
  • Peter Mortensen
    Peter Mortensen almost 3 years
    Why is JavaScript required?
  • ßãlãjî
    ßãlãjî almost 3 years
    javascript is not neccessary ,if u have static base64 url.you dirctly put into src
  • Kevin
    Kevin almost 3 years
    @PeterMortensen It's just plain JavaScript (with some ES6 syntax). You can run it anywhere. e.g. paste it in Chrome DevTools console, in HTML script tag, or in a .js file
  • Matthew Lock
    Matthew Lock almost 3 years
    Convert your image file to base64 in PowerShell deangrant.me/2014/10/01/…