51

I have two base64 encoded in PNG, and I need to compare them using Resemble.JS

I think that the best way to do it is to convert the PNG's into file objects using fileReader. How can I do it?

Mahdi Alkhatib
  • 1,954
  • 1
  • 29
  • 43
Bonik
  • 792
  • 1
  • 7
  • 15

3 Answers3

93

Way 1: only works for dataURL, not for other types of url.

function dataURLtoFile(dataurl, filename) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type:mime});
}

//Usage example:
var file = dataURLtoFile('data:image/png;base64,......', 'a.png');
console.log(file);

Way 2: works for any type of url, (http url, dataURL, blobURL, etc...)

//return a promise that resolves with a File instance
function urltoFile(url, filename, mimeType){
    mimeType = mimeType || (url.match(/^data:([^;]+);/)||'')[1];
    return (fetch(url)
        .then(function(res){return res.arrayBuffer();})
        .then(function(buf){return new File([buf], filename, {type:mimeType});})
    );
}

//Usage example:
urltoFile('data:image/png;base64,......', 'a.png')
.then(function(file){
    console.log(file);
})

Both works in Chrome and Firefox.

cuixiping
  • 24,167
  • 8
  • 82
  • 93
  • 1
    Works great, this should be the accepted answer, it's much simpler – Pablo Quemé May 29 '17 at 18:03
  • You can guess the MIME type and the extension by: ``` let matches = url.match(/^data:([^;]+);base64,(.*)$/); let mimeType = matches[1]; let extension = matches[1].substr(matches[1].indexOf('/')+1); ``` – C Würtz Dec 06 '17 at 17:14
  • "Way 2" works great on chrome and firefox but stoped working for me on safari(cordova UIWebview). This think is to what works on all three.https://stackoverflow.com/a/5100158/9909941 – tanner burton Dec 03 '19 at 01:12
  • Your answer is really useful, i just want to ask that can we perform compression ? if yes then please tell me how. – Chirag Joshi Apr 10 '20 at 04:33
43

You can create a Blob from your base64 data, and then read it asDataURL:

var img_b64 = canvas.toDataURL('image/png');
var png = img_b64.split(',')[1];

var the_file = new Blob([window.atob(png)],  {type: 'image/png', encoding: 'utf-8'});

var fr = new FileReader();
fr.onload = function ( oFREvent ) {
    var v = oFREvent.target.result.split(',')[1]; // encoding is messed up here, so we fix it
    v = atob(v);
    var good_b64 = btoa(decodeURIComponent(escape(v)));
    document.getElementById("uploadPreview").src = "data:image/png;base64," + good_b64;
};
fr.readAsDataURL(the_file);

Full example (includes junk code and console log): http://jsfiddle.net/tTYb8/


Alternatively, you can use .readAsText, it works fine, and its more elegant.. but for some reason text does not sound right ;)

fr.onload = function ( oFREvent ) {
    document.getElementById("uploadPreview").src = "data:image/png;base64,"
    + btoa(oFREvent.target.result);
};
fr.readAsText(the_file, "utf-8"); // its important to specify encoding here

Full example: http://jsfiddle.net/tTYb8/3/

c69
  • 19,951
  • 7
  • 52
  • 82
2

You can use axios, async/await with TypeScript.

const dataUrlToFile = async (dataUrl: string, fileName: string, mimeType: string): Promise<File> => {
  const res = await axios(dataUrl);
  const blob: Blob = res.data;
  return new File([blob], fileName, { type: mimeType });
}

Example of usage

const TEST_IMG_BASE64 = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'
const TEST_IMG: File = await dataUrlToFile(TEST_IMG_BASE64, 'test.gif', 'image/gif')
Fatih Bulut
  • 2,385
  • 1
  • 15
  • 12