其他分享
首页 > 其他分享> > 本地图片url转Base64

本地图片url转Base64

作者:互联网

  function  convertImgToBase64(url, callback, outputFormat) {
            var img = new Image;
            img.crossOrigin = "Anonymous"; //跨域
            img.src = url;
            img.onload = function () {
                var canvas = document.createElement('CANVAS');
                var ctx = canvas.getContext('2d');
                canvas.height = img.height;
                canvas.width = img.width;
                ctx.drawImage(img, 0, 0);
                var dataURL = canvas.toDataURL(outputFormat || 'image/png');
                callback.call(this, dataURL);
                canvas = null;
            };
        },

 

标签:canvas,dataURL,img,url,Base64,width,本地,var
来源: https://blog.csdn.net/Richardjgp/article/details/101456002