Try this:
/**
* Convert image
* to a base64 data uri
* @param {String} url
* @param {Function} callback
* @param {String} [outputFormat=image/png]
*/
function convertImageToDataURI(url, callback, outputFormat) {
var img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = function(){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'), dataURL;
canvas.height = this.height;
canvas.width = this.width;
ctx.drawImage(this, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
}
Usage
convertImageToDataURI('ImageLink', function(base64Decoded){
// Base64DataURI
});
Supported input formats
image/png, image/jpeg, image/jpg, image/gif, image/bmp, image/tiff, image/x-icon, image/svg+xml, image/webp, image/xxx
Supported output formats
image/png,image/jpeg,image/webp (chrome)
Demo:
fiddle
Test: toDataUrl mime type
http://kangax.github.io/jstests/toDataUrl_mime_type_test/
Browser Support (so far I know)
Images from the local file system
If you want to convert images from the users file system you need to take a different approach. Use the FileReader API (Check out this fiddle).