Skip to content

Instantly share code, notes, and snippets.

@necmettin
Created August 18, 2015 13:08
Show Gist options
  • Save necmettin/fa16bc70288ddf4de2bf to your computer and use it in GitHub Desktop.
Save necmettin/fa16bc70288ddf4de2bf to your computer and use it in GitHub Desktop.
Javascript Get-Image as Data String
function getDataUri(url, callback) {
var image = new Image();
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size
canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size
canvas.getContext('2d').drawImage(this, 0, 0);
callback(canvas.toDataURL('image/png').replace(/^data:image\/(png|jpg);base64,/, ''));
};
image.src = url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment