11 lines
548 B
JavaScript
11 lines
548 B
JavaScript
// Rôle : compresse les images utilisateur avant stockage IndexedDB.
|
|
export async function compressImage(file) {
|
|
const bitmap = await createImageBitmap(file);
|
|
const maxSide = 1400;
|
|
const ratio = Math.min(1, maxSide / Math.max(bitmap.width, bitmap.height));
|
|
const canvas = document.createElement("canvas");
|
|
canvas.width = Math.round(bitmap.width * ratio);
|
|
canvas.height = Math.round(bitmap.height * ratio);
|
|
canvas.getContext("2d").drawImage(bitmap, 0, 0, canvas.width, canvas.height);
|
|
return canvas.toDataURL("image/jpeg", 0.78);
|
|
}
|