Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vordan/9d497020feecd8672c66edc5138608fd to your computer and use it in GitHub Desktop.
Save vordan/9d497020feecd8672c66edc5138608fd to your computer and use it in GitHub Desktop.
function getBase64FromImageUrl(url) {
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");
alert(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
};
img.src = url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment