[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
This commit is contained in:
DevAstro
2025-06-30 20:04:37 +01:00
committed by GitHub
parent 591c98f3e6
commit a4b96ed37e
2 changed files with 14 additions and 5 deletions

View File

@@ -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: {

View File

@@ -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')
}