Skip to content

Instantly share code, notes, and snippets.

@odd-poet
Created October 16, 2014 14:13
Show Gist options
  • Save odd-poet/2babc0ec25309d500142 to your computer and use it in GitHub Desktop.
Save odd-poet/2babc0ec25309d500142 to your computer and use it in GitHub Desktop.
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