13

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.?

biziclop
  • 14,466
  • 3
  • 49
  • 65
Mike Vierwind
  • 1,482
  • 4
  • 23
  • 44

3 Answers3

16

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){

});
miensol
  • 39,733
  • 7
  • 116
  • 112
  • where is this dataUri saved? is it temporarily saving in a cache directory on the device starting with "data:text/plain;base64," or in my case "data:image/jpeg;base64," ? (Usage in react-native) – Blaze Gawlik Oct 15 '21 at 03:21
4

As a complete solution to your scenario, you can use fetch to get a blob representation of your image, and then use FileReader to convert the blob in its base64 representation

// get an image blob from url using fetch
let getImageBlob = function(url){
  return new Promise( async resolve=>{
    let resposne = await fetch( url );
    let blob = resposne.blob();
    resolve( blob );
  });
};

// convert a blob to base64
let blobToBase64 = function(blob) {
  return new Promise( resolve=>{
    let reader = new FileReader();
    reader.onload = function() {
      let dataUrl = reader.result;
      resolve(dataUrl);
    };
    reader.readAsDataURL(blob);
  });
}

// combine the previous two functions to return a base64 encode image from url
let getBase64Image = async function( url ){
  let blob = await getImageBlob( url );
  let base64 = await blobToBase64( blob );
  return base64;
}

// test time!
getBase64Image( 'http://placekitten.com/g/200/300' ).then( base64Image=> console.log( base64Image) );
colxi
  • 7,640
  • 2
  • 45
  • 43
  • 1
    This has the advantage of working in Safari, where URL.createObjectURL() has issues in later versions of Safari. – Blue Aug 07 '20 at 18:55
0

One way is to make a Blob of an object, and then use URL.createObjectURL()

let a = URL.createObjectURL(new Blob([JSON.stringify({whatever: "String..."}, null, 2)]))


console.log(a)
NVRM
  • 11,480
  • 1
  • 88
  • 87
  • 1
    This isn't serializable though, right? – Ted Brownlow Aug 02 '22 at 20:31
  • No. A blob is just a reference to the file, in the browser memory. To actually store the whole object or file in a URL/string, see if this post can help: https://stackoverflow.com/a/57299797/2494754 . There is also the old XML serializer that can help depending what you are doing https://developer.mozilla.org/en-US/docs/Web/API/XMLSerializer , or base64 Data URLs https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs – NVRM Aug 02 '22 at 21:13