Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convertImgToBase64
// 출처 : http://stackoverflow.com/questions/6150289/how-to-convert-image-into-base64-string-using-javascript
/**
* 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;
}
// Usage
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
You can’t perform that action at this time.