js 将远程跨域图片连接转换成base64格式 亲测可用
所属栏目: 网站开发 | 更新时间:2017-3-26 | 阅读:10860 次
网上找了很多方法,要么说JS不能转,要么就是转换不了,以下是找到的完美的解决方法,
function
convertImgToBase64(url, callback, outputFormat){
var
canvas = document.createElement(
'CANVAS'
),
ctx = canvas.getContext(
'2d'
),
img =
new
Image;
img.crossOrigin =
'Anonymous'
;
img.onload =
function
(){
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
;
};
img.src = url;
}
// Base64DataURL
alert(basearr[1]);
});
2018-01-02 15:50