Skip to content

Instantly share code, notes, and snippets.

@gonejack
Forked from oliyh/image-url-to-data-uri.js
Created August 7, 2017 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gonejack/24c96e825e30199d6c5a70943ce65781 to your computer and use it in GitHub Desktop.
Save gonejack/24c96e825e30199d6c5a70943ce65781 to your computer and use it in GitHub Desktop.
Convert an image url to a data URI without canvas
// hard won knowledge from http://stackoverflow.com/questions/20035615/using-raw-image-data-from-ajax-request-for-data-uri
var xmlHTTP = xhr.XMLHttpRequest();
xmlHTTP.open('GET', url, true);
xmlHTTP.responseType = 'arraybuffer';
xmlHTTP.onload = function(e) {
var arr = new Uint8Array(this.response);
var raw = String.fromCharCode.apply(null,arr);
var b64 = base64.encode(raw);
var dataURL="data:image/png;base64," + b64;
};
xmlHTTP.send();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment