Did anyone was successful in using npm package jimp or sharp for simple image resizing?

Hello,

I'm used the lambda function with the AI assistant to call a nmp package to do a simple resize of an image. I know the npm package integration is kind of new so I've tried, unsuccessfully, with that simple use of it.

A bit of context: In the webapp, users can upload a logo to show in the menu and I want to resize the image if users upload images that are too big. It's a simple transformation.

However, the assistant cannot seems to output a working code. I was surprised because sharp is an npm packaged mentioned in the code release from Xano itself. https://community.xano.com/release-announcements/post/r1-66-EbeL5HnYsybyriC

Did anyone had more success using this?

Here is the code that the AI assistant wrote:

const importedSharp = await import("npm:sharp");
const sharp = importedSharp.default || importedSharp;

const imagePath = $var.logo.path;

if (!imagePath) {
    throw new Error("Input image path ($var.logo.path) is missing or invalid.");
}

const maxWidth = 300;
const maxHeight = 150;

try {
    const resizedImageBuffer = await sharp(imagePath)
        .resize({
            width: maxWidth,
            height: maxHeight,
            fit: sharp.fit.inside,
            withoutEnlargement: true 
        })
        .toBuffer();

    return resizedImageBuffer;

} catch (error) {
    console.error("Error during image resizing with sharp:", error.message);
    throw new Error(`Failed to resize image at path '${imagePath}': ${error.message}`);
}

and the logo object
{"access":"public","path":"/vault/KpmcSsbZ/Q6u-M2nNeKaqNTHikLdheQYZ-5I/M2NFRg../1cc0e0dc60638a6b3a116ceb40a83106d21ed294.png","name":"1cc0e0dc60638a6b3a116ceb40a83106d21ed294.png","type":"image","size":35437,"mime":"image/png","meta":{"width":1221,"height":784}}

2 replies