Skip to content

Instantly share code, notes, and snippets.

@erdii
Last active March 31, 2016 09:00
Show Gist options
  • Save erdii/8fcc29d0bdb62e6c05b0503da8a45e35 to your computer and use it in GitHub Desktop.
Save erdii/8fcc29d0bdb62e6c05b0503da8a45e35 to your computer and use it in GitHub Desktop.
// spotted @ https://davidwalsh.name/convert-image-data-uri-javascript
function getDataUri(url, callback) {
var image = new Image();
image.onload = function () {
var canvas = document.createElement("canvas");
canvas.width = this.naturalWidth;
canvas.height = this.naturalHeight;
canvas.getContext("2d").drawImage(this, 0, 0);
var dataUrl = canvas.toDataURL("image/png");
callback(dataUrl);
};
image.src = url;
}
// use it like
getDataUri("url/to/image", function(dataUrl) {
// do something with dataUrl
console.log(dataUrl);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment