图片转成 base64
对于更多日常应用的公共类的操作方法,能够关注下小滑轮网站 http://www.feiaci.com/#/self/…
/**
* 蒋图片转成 base64
* width、height 调用时传入具体像素值,管制大小 , 不传则默认图像大小
* 能够会有跨域问题,倡议是同源
* @param imgSrc 图片地址
* @param width
* @param height
* @returns {string}
*/
function getBase64Image(imgSrc, width, height) {return new Promise((resolve) => {const newImg = new Image();
newImg.setAttribute('crossOrigin', 'anonymous');
newImg.src = imgSrc;
const canvas = document.createElement('canvas');
canvas.width = width || img.width;
canvas.height = height || img.height;
const ctx = canvas.getContext('2d');
newImg.onload = function () {ctx.drawImage(newImg, 0, 0, canvas.width, canvas.height);
const dataURL = canvas.toDataURL('image/png', 1);
resolve(dataURL);
};
});
}