Skip to content

Instantly share code, notes, and snippets.

@farskid
Created November 10, 2017 23:02
Show Gist options
  • Save farskid/068ccc316798ac76189e39b1d743025d to your computer and use it in GitHub Desktop.
Save farskid/068ccc316798ac76189e39b1d743025d to your computer and use it in GitHub Desktop.
Create base64 out of image in Javascript
function base64Image(src) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function() {
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL("image/png");
resolve(dataURL);
};
img.onerror = function(err) {
reject(err);
}
img.src = src;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment