export function compressPicture(file) {
return new Promise((resolve, reject) => {
let width, height;
let MAX_WH = 800;
let image = new Image()
image.onload = () => {
if (image.height > MAX_WH) {
image.width *= MAX_WH / image.height;
image.height = MAX_WH;
}
if (image.width > MAX_WH) {
image.height *= MAX_WH / image.width;
image.width = MAX_WH;
}
let canvas = document.createElement('canvas');
let context = canvas.getContext('2d');
canvas.width = width = image.width;
canvas.height = height = image.height;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(image, 0, 0, image.width, image.height);
const base64Url = canvas.toDataURL(file.type, 0.3);
resolve(base64Url)
}
image.onerror = () => {
reject()
}
image.src = file.url;
})
}