mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: return og dimensions when generating thumb
This commit is contained in:
@@ -41,7 +41,7 @@ export const getUrlInfo = async(
|
|||||||
let jpegThumbnail: Buffer | undefined = undefined
|
let jpegThumbnail: Buffer | undefined = undefined
|
||||||
try {
|
try {
|
||||||
jpegThumbnail = image
|
jpegThumbnail = image
|
||||||
? await getCompressedJpegThumbnail(image, opts)
|
? (await getCompressedJpegThumbnail(image, opts)).buffer
|
||||||
: undefined
|
: undefined
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
logger?.debug(
|
logger?.debug(
|
||||||
|
|||||||
@@ -97,20 +97,35 @@ export const extractImageThumb = async(bufferOrFilePath: Readable | Buffer | str
|
|||||||
|
|
||||||
const lib = await getImageProcessingLibrary()
|
const lib = await getImageProcessingLibrary()
|
||||||
if('sharp' in lib) {
|
if('sharp' in lib) {
|
||||||
const result = await lib.sharp!.default(bufferOrFilePath)
|
const img = lib.sharp!.default(bufferOrFilePath)
|
||||||
|
const dimensions = await img.metadata()
|
||||||
|
|
||||||
|
const buffer = await img
|
||||||
.resize(width)
|
.resize(width)
|
||||||
.jpeg({ quality: 50 })
|
.jpeg({ quality: 50 })
|
||||||
.toBuffer()
|
.toBuffer()
|
||||||
return result
|
return {
|
||||||
|
buffer,
|
||||||
|
original: {
|
||||||
|
width: dimensions.width,
|
||||||
|
height: dimensions.height,
|
||||||
|
},
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp
|
const { read, MIME_JPEG, RESIZE_BILINEAR, AUTO } = lib.jimp
|
||||||
|
|
||||||
const jimp = await read(bufferOrFilePath as any)
|
const jimp = await read(bufferOrFilePath as any)
|
||||||
const result = await jimp
|
const buffer = await jimp
|
||||||
.quality(50)
|
.quality(50)
|
||||||
.resize(width, AUTO, RESIZE_BILINEAR)
|
.resize(width, AUTO, RESIZE_BILINEAR)
|
||||||
.getBufferAsync(MIME_JPEG)
|
.getBufferAsync(MIME_JPEG)
|
||||||
return result
|
return {
|
||||||
|
buffer,
|
||||||
|
original: {
|
||||||
|
width: jimp.getWidth(),
|
||||||
|
height: jimp.getHeight()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,8 +239,8 @@ export async function generateThumbnail(
|
|||||||
) {
|
) {
|
||||||
let thumbnail: string | undefined
|
let thumbnail: string | undefined
|
||||||
if(mediaType === 'image') {
|
if(mediaType === 'image') {
|
||||||
const buff = await extractImageThumb(file)
|
const { buffer } = await extractImageThumb(file)
|
||||||
thumbnail = buff.toString('base64')
|
thumbnail = buffer.toString('base64')
|
||||||
} else if(mediaType === 'video') {
|
} else if(mediaType === 'video') {
|
||||||
const imgFilename = join(getTmpFilesDirectory(), generateMessageID() + '.jpg')
|
const imgFilename = join(getTmpFilesDirectory(), generateMessageID() + '.jpg')
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user