Lib: Update server-side image compression

This commit is contained in:
2025-02-17 18:14:18 +01:00
parent bd7e962f83
commit 6093553ce5

View File

@ -7,17 +7,21 @@ import sharp from "sharp";
*/ */
export const image_to_avif = async ( export const image_to_avif = async (
data: ArrayBuffer, data: ArrayBuffer,
width: number | undefined = undefined, width?: number,
height: number | undefined = undefined, height?: number,
quality: number = 50, quality: number = 50,
effort: number = 4, effort: number = 4,
): Promise<Blob> => { ): Promise<Blob> => {
console.log(
`Compressing ${data.byteLength} Bytes to ${width ?? -1}x${height ?? -1} avif with quality ${quality} and effort ${effort}...`,
);
const compressed: Buffer = await sharp(data) const compressed: Buffer = await sharp(data)
.resize(width, height) .resize(width, height)
.avif({ quality: quality, effort: effort }) .avif({ quality: quality, effort: effort })
.toBuffer(); .toBuffer();
console.log(`image_to_avif: ${data.byteLength} Bytes -> ${compressed.length} Bytes`); console.log(`Compressed ${data.byteLength} Bytes to ${compressed.length} Bytes`);
return new Blob([compressed]); return new Blob([compressed]);
}; };