Created
August 18, 2015 13:08
-
-
Save necmettin/fa16bc70288ddf4de2bf to your computer and use it in GitHub Desktop.
Javascript Get-Image as Data String
This file contains 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 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