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;
}
convertImgToBase64('http://runjs.cn/img/logo.png', function(base64Img){
console.log(base64Img);
// Base64DataURL
});
使用canvas 将img 的url 转换为base64格式
最新推荐文章于 2024-05-20 22:28:51 发布