compressImage(picPath, w, callback) {
const image = new Image();
let quality = 0.6;
image.src = picPath;
image.setAttribute('crossOrigin', 'Anonymous');
let imgWidth;
let imgHeight;
image.onload = function () {
imgWidth = this.width;
imgHeight = this.height;
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (Math.max(imgWidth, imgHeight) > w) {
if (imgWidth > imgHeight) {
canvas.width = w;
canvas.height = (w * imgHeight) / imgWidth;
} else {
canvas.height = w;
canvas.width = (w * imgWidth) / imgHeight;
}
} else {
canvas.width = imgWidth;
canvas.height = imgHeight;
quality = 0.6;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
const base64 = canvas.toDataURL('image/jpeg', quality);
callback(base64);
};
},
this.compressImage(picPath, 800, (base64) => {
});