From 9f8223d46bc1b8deb1e1e0cc7fdc7eb334a1060d Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Thu, 2 Dec 2021 12:13:45 +0530 Subject: [PATCH] fix: 10x better compression for image thumbs --- src/Utils/messages-media.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Utils/messages-media.ts b/src/Utils/messages-media.ts index 78b9df2..e20b847 100644 --- a/src/Utils/messages-media.ts +++ b/src/Utils/messages-media.ts @@ -55,9 +55,12 @@ export const compressImage = async (bufferOrFilePath: Readable | Buffer | string if(bufferOrFilePath instanceof Readable) { bufferOrFilePath = await toBuffer(bufferOrFilePath) } - const { read, MIME_JPEG } = await import('jimp') + const { read, MIME_JPEG, RESIZE_BILINEAR } = await import('jimp') const jimp = await read(bufferOrFilePath as any) - const result = await jimp.resize(32, 32).getBufferAsync(MIME_JPEG) + const result = await jimp + .quality(50) + .resize(32, 32, RESIZE_BILINEAR) + .getBufferAsync(MIME_JPEG) return result } export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => { @@ -70,12 +73,15 @@ export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => { bufferOrFilePath = await toBuffer(mediaUpload.stream) } - const { read, MIME_JPEG } = await import('jimp') + const { read, MIME_JPEG, RESIZE_BILINEAR } = await import('jimp') const jimp = await read(bufferOrFilePath as any) const min = Math.min(jimp.getWidth (), jimp.getHeight ()) const cropped = jimp.crop (0, 0, min, min) return { - img: await cropped.resize(640, 640).getBufferAsync(MIME_JPEG), + img: await cropped + .quality(50) + .resize(640, 640, RESIZE_BILINEAR) + .getBufferAsync(MIME_JPEG), } } /** gets the SHA256 of the given media message */