When to Consider Base64 (Data: Uri) Images

How to display Base64 images in HTML

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>

Image to data uri without base64

It's definitely possible to render image without base64 encoding.

Here is it:

//function for convert byte array to hexfunction toHexString(byteArray) {  return Array.from(byteArray, function(byte) {    return ('0' + (byte & 0xFF).toString(16)).slice(-2);  }).join('%')}
//image in base64var imgB64="Qk3KAAAAAAAAAD4AAAAoAAAAHQAAACMAAAABAAEAAAAAAIwAAADDDgAAww4AAAAAAAAAAAAAAAAAAP///wD//f/4//B/+P/tv/j/nc/4/n3z+P39/fjx+Px45uUzuJddz8h3vd/wd73f8He93/BvvN/wX78f8D+f3/BOZz/wdfr/8Hv9//B7+f/we+X/8Hvd//B73f/we93/8GTd/nBfUniQP4+m6L/f3tjP396489/eeP3f2fj+X9f4/5/P+P/Pv/j/8n/4//3/+A";
//base64 to stringvar imgStr=atob(imgB64);
//make byte arrayvar bytes=Uint8Array.from(imgStr, c => c.charCodeAt(0));
//transform input array into hex arrayvar hexBytes = "data:image,%" + toHexString(bytes);
//set image to elementdocument.getElementById("pic").src = hexBytes;
<html>  <head>    <title>Sample</title>  </head>
<body> <img id="pic"> </body></html>

What is the effect of encoding an image in base64?

It will be approximately 37% larger:

Very roughly, the final size of Base64-encoded binary data is equal to 1.37 times the original data size

Source: http://en.wikipedia.org/wiki/Base64

Why use data URI scheme?

According to Wikipedia:

Advantages:

  • HTTP request and header traffic is not required for embedded data, so
    data URIs consume less bandwidth whenever the overhead of encoding
    the inline content as a data URI is smaller than the HTTP overhead.
    For example, the required base64 encoding for an image 600 bytes long
    would be 800 bytes, so if an HTTP request required more than 200
    bytes of overhead, the data URI would be more efficient.

  • For transferring many small files (less than a few kilobytes each), this can be faster. TCP transfers tend to start slowly. If each file requires a new TCP connection, the transfer speed is limited by the round-trip time rather than the available bandwidth. Using HTTP keep-alive improves the situation, but may not entirely alleviate the bottleneck.

  • When browsing a secure HTTPS web site, web browsers commonly require that all elements of a web page be downloaded over secure connections, or the user will be notified of reduced security due to a mixture of secure and insecure elements. On badly configured servers, HTTPS requests have significant overhead over common HTTP requests, so embedding data in data URIs may improve speed in this case.

  • Web browsers are usually configured to make only a certain number
    (often two) of concurrent HTTP connections to a domain, so inline
    data frees up a download connection for other content.

  • Environments with limited or restricted access to external resources
    may embed content when it is disallowed or impractical to reference
    it externally. For example, an advanced HTML editing field could
    accept a pasted or inserted image and convert it to a data URI to
    hide the complexity of external resources from the user.
    Alternatively, a browser can convert (encode) image based data from
    the clipboard to a data URI and paste it in a HTML editing field.
    Mozilla Firefox 4 supports this functionality.

  • It is possible to manage a multimedia page as a single file. Email
    message templates can contain images (for backgrounds or signatures)
    without the image appearing to be an "attachment".

Disadvantages:

  • Data URIs are not separately cached from their containing documents
    (e.g. CSS or HTML files) so data is downloaded every time the
    containing documents are redownloaded. Content must be re-encoded and
    re-embedded every time a change is made.

  • Internet Explorer through version 7 (approximately 15% of the market as of January 2011), lacks support. However this can be overcome by serving browser specific content.
    Internet Explorer 8 limits data URIs to a maximum length of 32 KB.

  • Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.

  • Base64-encoded data URIs are 1/3 larger in size than their binary
    equivalent. (However, this overhead is reduced to 2-3% if the HTTP
    server compresses the response using gzip) Data URIs make it more
    difficult for security software to filter content.

According to other sources
- Data URLs are significantly slower on mobile browsers.

what does this mean ? image/png;base64?

It's an inlined image (png), encoded in base64. It can make a page faster: the browser doesn't have to query the server for the image data separately, saving a round trip.

(It can also make it slower if abused: these resources are not cached, so the bytes are included in each page load.)

Is embedding background image data into CSS as Base64 good or bad practice?

It's not a good idea when you want your images and style information to be cached separately. Also if you encode a large image or a significant number of images in to your css file it will take the browser longer to download the file leaving your site without any of the style information until the download completes. For small images that you don't intend on changing often if ever it is a fine solution.

as far as generating the base64 encoding:

  • http://b64.io/
  • http://www.motobit.com/util/base64-decoder-encoder.asp (upload)
  • http://www.greywyvern.com/code/php/binary2base64 (from link with little tutorials underneath)

Why do I see websites using (really long) base 64 pictures in their HTML?

One advantage of data URLs is that the resulting image is available immediately without another round trip to the server. Some disadvantages of this method are that your image is not cached, and for larger images the encoded url can become quite long.

Source: MDN - Using Images

Use Base64 String from URL in src tag of image

It is not working because you are treating a page featuring a Data URL string, as if were just another type of external link-able image asset. Unfortunately linking to an external asset works for image files, but Data URLs are meant as an alternative to an external link, and thus does not work in the same way.

In short, to display an image making use of a data URL string, you need put the actual data URL string as the src= value, in your case for example:

<img alt="" src="data:image/gif;base64,iVBORw0KGgo ...  " style="height:836px; width:592px">

Examples

Example HTML from Masinter, 1998 RFC 2397 - The "data" URL scheme:

<IMG SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAw AAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFz ByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSp a/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJl ZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uis F81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PH hhx4dbgYKAAA7" ALT="Larry">


Related Topics



Leave a reply



Submit