Created
September 7, 2014 09:22
-
-
Save alihalabyah/1e01721ea007b52cbe3a to your computer and use it in GitHub Desktop.
convert image to base64
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
/** | |
* Convert an image | |
* to a base64 string | |
* @param {String} url | |
* @param {Function} callback | |
* @param {String} [outputFormat=image/png] | |
*/ | |
function convertImgToBase64(url, callback, outputFormat){ | |
var canvas = document.createElement('CANVAS'), | |
ctx = canvas.getContext('2d'), | |
img = new Image; | |
img.crossOrigin = 'Anonymous'; | |
img.onload = function(){ | |
var dataURL; | |
canvas.height = img.height; | |
canvas.width = img.width; | |
ctx.drawImage(img, 0, 0); | |
dataURL = canvas.toDataURL(outputFormat); | |
callback.call(this, dataURL); | |
canvas = null; | |
}; | |
img.src = url; | |
} | |
convertImgToBase64('http://bit.ly/18g0VNp', function(base64Img){ | |
// Base64DataURL | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment