Search code examples
javascriptnode.jsimageimage-processinggraphicsmagick

Graphics Magick with Node - creating a cropped thumbnail without reducing image quality


I have some code which crops an image and centers it using Graphics Magick. However the resulting image has a reduced fidelity. I'm wondering if anyone knows a way to avoid the reduction in quality?

  gm(imagePath)
    .thumbnail(25, 25 + '^')
    .gravity('Center')
    .extent(25, 25)
    .write(imagePath, function (error) {
      if (error) console.log('Error - ', error);
      callback()
    });

Solution

  • Add the .quality() method to your chainStack, like this:

     gm(imagePath)
        .thumbnail(25, 25 + '^')
        .quality(100)
        .gravity('Center')
        .extent(25, 25)
        .write(imagePath, function (error) {
          if (error) console.log('Error - ', error);
          callback()
        });
    

    Of course you can play with the quality % to suit your needs.

    Here is the function reference:

    http://aheckmann.github.io/gm/docs.html#quality http://www.graphicsmagick.org/GraphicsMagick.html#details-quality