Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create data URIs on the fly?

Is there a script (javascript / client side). That create data URIs on the fly. Now i create data URIs with a online base64 creator. And then put that output in the css file. But when i changing the images. Its a lot of work to do it. Is there a script, that can do it for me.?

like image 452
Mike Vierwind Avatar asked Oct 21 '11 06:10

Mike Vierwind


People also ask

How does data URI work?

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.

What type of data is URI?

A Data URI is essentially binary data encoded into a Base64 format, along with some additional information for the browser including a MIME type, a Charset and the encoding format (Base64).

How data is used in URI images?

Data URI is an Uniform Resource Identifier scheme that provides a way to include data in-line in webpages. You need to copy & paste the Data URI as input and you can save the output image file. Note : For reverse conversion, use Image to DataURI Converter. Note : For reverse conversion, use Image to DataURI Converter.


1 Answers

The modern browsers now have good support for base64 encoding and decoding. There are two functions respectively for decoding and encoding base64 strings:

  • atob() decodes a string of base-64 data
  • btoa() creates a base-64 encoded ASCII string from a "string" of binary data

This let's you create data uri's easily i.e

var longText = "Lorem ipsum....";
var dataUri = "data:text/plain;base64," + btoa(longText);
//a sample api expecting an uri
d3.csv(dataUri, function(parsed){

});
like image 83
miensol Avatar answered Oct 06 '22 23:10

miensol