I am trying to find a suitable library for image processing,
Basically, I have a use-case for Resizing, Compressing and creating thumbnails of an image,
I found that sharp seems to be a popular node library choice for image processing,
I am able to do some basic operations like resizing an image but,
I couldn't find a way to create thumbnails or smaller sized quality images of the original one.
Can someone please point to the correct code for creating thumbnails and smaller resolution images using the sharp library?
Sample working code:
const sharp = require('sharp');
let test = async () => {
await sharp('/pathToImage/test.jpg')
.resize({
fit: sharp.fit.outside
})
.sharpen()
.toFile('fitOutside.jpg')
.then(info => {
console.log(info);
})
.catch(err => {
console.log(err);
});
};
test();
Reference -
http://sharp.pixelplumbing.com/en/stable/api-resize/
https://sharp.pixelplumbing.com/en/stable/api-resize/#examples_2
If you give both width and height, sharp will usually need to either add or remove pixels on one axis. You can control what it does with the fit parameter:
http://sharp.pixelplumbing.com/en/stable/api-resize/
The default is centre, it sounds like you'd prefer outside.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With