fazlurr
8/23/2016 - 5:33 AM

Get Image base64 encoded data from URL - http://stackoverflow.com/a/16566198

Get Image base64 encoded data from URL - http://stackoverflow.com/a/16566198

function getBase64FromImageUrl(url) {
    var img = new Image();

    img.setAttribute('crossOrigin', 'anonymous');

    img.onload = function () {
        var canvas = document.createElement("canvas");
        canvas.width =this.width;
        canvas.height =this.height;

        var ctx = canvas.getContext("2d");
        ctx.drawImage(this, 0, 0);

        var dataURL = canvas.toDataURL("image/png");

        var base64 = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
        
        return base64;
    };

    img.src = url;
}