Skip to content

Instantly share code, notes, and snippets.

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 arronmabrey/bf70d8ef70251b26c076e30a4b9b0e01 to your computer and use it in GitHub Desktop.
Save arronmabrey/bf70d8ef70251b26c076e30a4b9b0e01 to your computer and use it in GitHub Desktop.
getBase64Image
function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
var base64 = getBase64Image(document.getElementById("imageid"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment