javascriptlambdaimagemagickgraphicsmagickgm

GraphicsMagick (GM) resize into square without changing ratio and add background


I'm trying to get GraphicsMagick to resize my various images into a standard square. The input is always variable - vertical, horizontal images, different sizes etc.

I want to basically have: - A white background canvas @ 600px x 600px - The image sits in the center of that canvas @ 500px x 500px

I've tried so many from the docs but I can't get it working correctly.

This is what I have working so far (JavaScript):

gm(content)
  .autoOrient()
  .resize(600, 600)
  .gravity('Center')
  .extent([600, 600])
  .background('#FFFFFF')
  .flatten();

And it just comes out the correct width, but keeps the ratio so for a rectangle, it comes out at 600px wide and 240px high (as it kept the ratio).

Any help appreciated!


Solution

  • The solution to my issue was simply that I set extent as an array. Final solution:

    gm(content)
      .autoOrient()
      .resize(550, 550)
      .gravity('Center')
      .background('#FFFFFF')
      .extent(600, 600)
      .flatten();