Try:
gm(readStream, fileObj.name()).autoOrient().resize('100', '100', "^").gravity('Center').extent('100', '100').stream().pipe(writeStream);
The autoOrient()
will automatically turn any mobile-phone photos to the correct direction (otherwise they sometimes come in sideways). The ^
that gets passed to resize
is an option that makes the "smallest edge" of your image resize to 100. So this will leave an "overhang" on the "correct" edge. It takes care of your image being really wide or really tall. The extant
then crops your image removing the overhang. The gravity('Center')
makes the cropping effect of extant
occur at the center of the image.
The only the thing I still haven't figured out is how to crop from the center of the image. According to most examples, calling gravity('center')
should do this, but it's not working for me. It always crops from the corner. It's working, it just doesn't look the best as most subject matter is in the center of an image.
EDIT: The gravity
call needs 'Center'
to work, not 'center'
. The call now works as intended. Also added autoOrient()
call.
Here's the official gm doc explaining the options. But it took researching how to do this in gm for node.js to figure the above syntax.