From a4b96ed37e119569101e6ec326e853c511f2d20f Mon Sep 17 00:00:00 2001 From: DevAstro Date: Mon, 30 Jun 2025 20:04:37 +0100 Subject: [PATCH] [READY FOR MERGE] Add support for custom profile picture dimensions (#1563) * Add support for custom profile picture dimensions The updateProfilePicture and generateProfilePicture functions now accept an optional dimensions parameter, allowing custom width and height for profile pictures. This provides more flexibility for image resizing beyond the default 640x640 size. * minor corrections Replaces 'w' and 'h' with 'width' and 'height' in profile picture related functions for improved clarity and consistency. * fix lint --- src/Socket/chats.ts | 8 ++++++-- src/Utils/messages-media.ts | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index bed2f79..856d8cb 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -257,7 +257,11 @@ export const makeChatsSocket = (config: SocketConfig) => { } /** update the profile picture for yourself or a group */ - const updateProfilePicture = async (jid: string, content: WAMediaUpload) => { + const updateProfilePicture = async ( + jid: string, + content: WAMediaUpload, + dimensions?: { width: number; height: number } + ) => { let targetJid if (!jid) { throw new Boom( @@ -269,7 +273,7 @@ export const makeChatsSocket = (config: SocketConfig) => { targetJid = jidNormalizedUser(jid) // in case it is someone other than us } - const { img } = await generateProfilePicture(content) + const { img } = await generateProfilePicture(content, dimensions) await query({ tag: 'iq', attrs: { diff --git a/src/Utils/messages-media.ts b/src/Utils/messages-media.ts index ebfe3a3..6bbf072 100644 --- a/src/Utils/messages-media.ts +++ b/src/Utils/messages-media.ts @@ -139,7 +139,12 @@ export const extractImageThumb = async (bufferOrFilePath: Readable | Buffer | st export const encodeBase64EncodedStringForUpload = (b64: string) => encodeURIComponent(b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/\=+$/, '')) -export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => { +export const generateProfilePicture = async ( + mediaUpload: WAMediaUpload, + dimensions?: { width: number; height: number } +) => { + const { width: w = 640, height: h = 640 } = dimensions || {} + let bufferOrFilePath: Buffer | string if (Buffer.isBuffer(mediaUpload)) { bufferOrFilePath = mediaUpload @@ -154,7 +159,7 @@ export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => { if ('sharp' in lib && typeof lib.sharp?.default === 'function') { img = lib.sharp .default(bufferOrFilePath) - .resize(640, 640) + .resize(w, h) .jpeg({ quality: 50 }) @@ -165,7 +170,7 @@ export const generateProfilePicture = async (mediaUpload: WAMediaUpload) => { const min = Math.min(jimp.getWidth(), jimp.getHeight()) const cropped = jimp.crop(0, 0, min, min) - img = cropped.quality(50).resize(640, 640, RESIZE_BILINEAR).getBufferAsync(MIME_JPEG) + img = cropped.quality(50).resize(w, h, RESIZE_BILINEAR).getBufferAsync(MIME_JPEG) } else { throw new Boom('No image processing library available') }