Skip to content

Instantly share code, notes, and snippets.

@mlconnor
Created June 17, 2013 15:20
Show Gist options
  • Save mlconnor/5797724 to your computer and use it in GitHub Desktop.
Save mlconnor/5797724 to your computer and use it in GitHub Desktop.
Convert image in a web page to base64 dataURL
function imgToDataUrl(imgEl) {
var canvas = document.createElement("canvas");
canvas.width = imgEl.width;
canvas.height = imgEl.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(imgEl, 0, 0, imgEl.width, imgEl.height);
// Get the data-URL formatted image
var dataURL = canvas.toDataURL("image/png");
return dataURL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment