// 压缩上传图片 export function translate(imgSrc, scale, callback) { let img = new Image(); img.src = imgSrc; img.onload = function() { let that = this; let h = that.height; // 默认按比例压缩 let w = that.width; let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); let width = document.createAttribute("width"); width.nodeValue = w; let height = document.createAttribute("height"); height.nodeValue = h; canvas.setAttributeNode(width); canvas.setAttributeNode(height); ctx.drawImage(that, 0, 0, w, h); var base64 = canvas.toDataURL('image/jpeg', scale); //压缩比例 canvas = null; callback(base64); } }