Resize and crop image and keeping aspect ratio NodeJS & gm

后端 未结 5 1222
囚心锁ツ
囚心锁ツ 2020-12-23 17:18

I\'ve been trying to create some thumbnails using the gm package from NodeJS, but I\'m out of lucky. I need to resize images bigger than 600x600 (could be any w

5条回答
  •  醉话见心
    2020-12-23 17:54

    To achieve a resized, cropped image with a center gravity with the gm module, you can use something similar to this:

    gm('/path/to/image.jpg')
      .resize('200', '200', '^')
      .gravity('Center')
      .crop('200', '200')
      .write(writeStream, function (err) {
        if (!err) console.log(' hooray! ');
      });
    

    The '^' argument on the resize function will tell GraphicsMagick to use the height and width as a minimum instead of the default behavior, maximum. The resulting resized image will have either the width or height be your designated dimension, while the non-conforming dimension is larger than the specified size.

    Then gravity function tells GraphicsMagick how the following crop function should behave, which will crop the image to the final size.

    You can find detailed documentation for the GraphicsMagick options used by the gm module here: http://www.graphicsmagick.org/GraphicsMagick.html

提交回复
热议问题