Nodejs sharp-> get metadata and resize image

114 views Asked by At

I'm currently working with Sharp version 0.32.4 to obtain image sizes and subsequently resize them to 50% of their original dimensions using the resize function.

Here's a snippet of my code:

    const transformer = sharp();
    thumbnail.pipe(transformer);
    const { width, height } = await transformer.metadata();
    logger.info("Original width and height:", width, height);
    const newWidth = width / 2;
    const newHeight = height / 2;
    const chain1 = transformer.resize(newWidth, newHeight)
                              .toFormat(config.imageThumbnail.format);
    receivedFile.push({
    id,
    type: "thumbnail",
    name: thumbName,
    stream: chain1
    });

This code performs well with very small images (around 20 KB), but I encounter issues when processing larger images. Specifically, the process seems to hang at the metadata function call, as indicated by the absence of the log print statement that follows it.

Could someone guide me on how to correctly handle images resize base on image metadata or suggest any adjustments to my current approach?

Thank you!

0

There are 0 answers