1、将图片转换为base64并压缩
// 将图片转换为base64并压缩 export const convertImgToBase64 = (url) => { return new Promise(function (resolve, reject) { const canvas = document.createElement('canvas') const ctx = canvas.getContext('2d') let img = document.createElement('img') img.onload = () => { const width = img.width const height = img.height let widthRate = width let heightRate = height if (width > 100) { // 限制大小100kb widthRate = 100 heightRate = 100 * height / width } // 按比例压缩4倍 canvas.width = widthRate canvas.height = heightRate ctx.drawImage(img, 0, 0, width, height, 0, 0, widthRate, heightRate) let u = canvas.toDataURL('image/png') u = u.replace(/^data:image\/(gif|png|jpg|jpge|bmp);base64,/, '') resolve(u) } img.src = url }) }