0

I am using imageMagic module to crop image here is my code:-

var imageMagick = require('imagemagick');
 imageMagick.resize({
    width: 50,
    height: 50,
    strip: false,
    srcPath: options.uploadDir + '/' + fileInfo.name,
    dstPath: options.uploadDir + '/' + version + '/' + fileInfo.name
}, finish);

it is working fine and crop the image but problem is when I set the size 50x50 but when image cropped I got the image size 50x38. I don't know what is the issue. Please help me to solve this issue. Thanks

mikefrey
  • 4,653
  • 3
  • 29
  • 40
Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68

1 Answers1

1

EDIT:

There is also a better method to do everything with a single statement:

imageMagick.resize({
    srcData: img,
    strip: false,
    width: 50,
    height: "50^",
    customArgs: [
        "-gravity", "center", "-extent", "50x50"
    ]
}, finish);

If you need a square from a rectangle you should first of all, crop the image at the size of the minimum side.

For example, if you have an image of 1024x768, find the width and height, then find the minimum side (768), then do a crop like this:

imageMagick.crop({
    width: 768,
    height: 768,
    srcPath: options.uploadDir + '/' + fileInfo.name,
    dstPath: options.uploadDir + '/' + version + '/' + fileInfo.name
}, finish);

Then you can resize it to a square like 50x50:

imageMagick.resize({
    width: 50,
    height: 50,
    srcPath: options.uploadDir + '/' + fileInfo.name,
    dstPath: options.uploadDir + '/' + version + '/' + fileInfo.name
}, finish);

I think you can chain resize and crop in a unique statement

michelem
  • 14,430
  • 5
  • 50
  • 66
  • 1
    yes now m already using this from http://stackoverflow.com/questions/21591536/resize-and-crop-image-and-keeping-aspect-ratio-nodejs-gm thanks for your help :) – Umesh Sehta Jun 30 '15 at 12:21
  • 1
    http://www.jeff.wilcox.name/2011/10/node-express-imagemagick-square-resizing/ thanks – Umesh Sehta Jun 30 '15 at 12:28