图片链接转成base64

时间:2021-05-14 17:14:48

一半需要我的图像转换为base64字符串,这样我们可以把我的形象到服务器。现在我们提供一个js:

function convertImgToBase64(url, callback, outputFormat){var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img =newImage;
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://bit.ly/18g0VNp',function(base64Img){// Base64DataURL});