3

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

4
  • Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. stackoverflow.com/help/on-topic Commented Aug 24, 2019 at 17:14
  • I don't understand your question. Just resize down to eg. 128 x 128 and save as a jpg. Isn't that a thumbnail? Commented Aug 26, 2019 at 4:45
  • I tried to resize by setting the desired dimensions but, it's removing a part of the image. I need a good quality small-sized version of the original HQ image Commented Aug 26, 2019 at 4:50
  • I wrote a blog on this medium.com/@pprathameshmore/… Commented Apr 7, 2020 at 10:38

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Updated the question, I just tried using fit: sharp.fit.outside without specifying the dimensions, but in this case, it kept the original size? Should I specify the width and height parameters?
You have two options: 1. Just give width and it'll resize to that width while letting height float. 2. Give width and height, and specify how you'd like the image to be fitted.
For case -> 1. I specify the width and fit correct? and for case 2. I specify the width, height and fit: sharp.fit.outside
No, you only need fit if you give both width and height.
then case 2. is correct yea? for case 1. only specify width?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.