Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank image encoded as data-uri [duplicate]

I've built an image slider (based on the terrific bxSlider) which will preload images just-in-time before they slide into view. It's working pretty well already, but I don't think my solution is valid HTML.

My technique is as follows: I generate the slider markup with the first slide image being inserted as usual (with an <img src="foo.jpg">) and subsequent images being referenced in a data attribute like <img data-orig="bar.jpg">. A Javascript then juggles the data-orig -> src change when necessary, triggering the preloading.

In other words, I have:

<div class="slider">
    <div><img src="time.jpg" /></div> 
    <div><img src="data:" data-orig="fastelavn.jpg" /></div> 
    <div><img src="data:" data-orig="pels_strik.jpg" /></div> 
    <div><img src="data:" data-orig="fashion.jpg" /></div> 
</div>

To avoid empty src="" attributes (which are harmful to performance in some browsers), I've inserted src="data:" to effectively insert a blank image as a placeholder.

The thing is, I can't seem to find anything in the documentation for data-URI saying whether this is a valid data-URI or not. I basically want the minimal data-URI that resolves to a blank/transparent image, so the browser can resolve the src immediately and move on (with no error or network request). Maybe src="data:image/gif;base64," would be better?

like image 608
Jens Roland Avatar asked Feb 03 '12 09:02

Jens Roland


People also ask

Is Base64 URL safe?

By consisting only of ASCII characters, base64 strings are generally url-safe, and that's why they can be used to encode data in Data URLs.

What is Base64 URI?

A data URI is a base64 encoded string that represents a file. Getting the contents of a file as a string means that you can directly embed the data within your HTML or CSS code. When the browser encounters a data URI in your code, it's able to decode the data and construct the original file.

How do I copy an image from URI?

Right click on an image preview within the Resources Panel to copy it as a Data URI (base 64 encoded).

What is data image Base64?

Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.


3 Answers

I looked into it and the smallest possible transparent GIF image, encoded as a data-uri, was this:

data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==

which is what I'm using now.

like image 55
Jens Roland Avatar answered Oct 04 '22 07:10

Jens Roland


If you need a transparent image 1x1 pixel, just set this data uri as src default attribute (keep the /// parts, it encodes byte 255, not a comment).

data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==

This is instead a base64 encoding for an image 1x1 white.

data:image/gif;base64,R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==

Otherwise you could set data:null and save ~60 extra bytes for each image.

like image 26
Fabrizio Calderan Avatar answered Oct 04 '22 05:10

Fabrizio Calderan


data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"/>

Valid and highly compressible. Essentially free if there's another inline svg in the page.

like image 21
Adria Avatar answered Oct 04 '22 06:10

Adria