Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ShapeGroup/abc785b1c47c83c17c76182f71f6f65c to your computer and use it in GitHub Desktop.
Save ShapeGroup/abc785b1c47c83c17c76182f71f6f65c to your computer and use it in GitHub Desktop.
js-kit-image-url-to-base-sixty-four
//// JS KIT - read a in b64 an image url
//// MIT LICENCE
//// Credit: alberto marà
// how to use:
// imageurltob64('jpg', paths.uploads + "staff/" + gud.picture, img64data => {
// yourimage.src = img64data;
// });
const imageurltob64 = (ext,url,back) => {
let img = new Image();
img.setAttribute('crossOrigin', 'anonymous');
img.onload = function() {
let canvas, ctx, dataURL;
canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(string('image/'+ext));
return back(dataURL);
};
img.src = url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment