Created
June 17, 2013 15:20
-
-
Save mlconnor/5797724 to your computer and use it in GitHub Desktop.
Convert image in a web page to base64 dataURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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