Search code examples
imageresizer

Add horizontal padding if the original image is less than the width specified


I don't think this is possible out of the box but wanted to make sure. We'd like to do the following.

Take any image input and force the output width to be a fixed size. If the width is less than the output width, we'd like to center the image and add horizontal padding to the image but not add vertical padding.

For example

  1. Original image is 700px x 400px
  2. Final output size of 1000px width x 400px. This would include 150px padding left and 150px padding right (no top / bottom padding).

I know that we can upscale the image (scale=both) or set the canvas scale (but that adds top / bottom padding) or we could add padding to the image but none are really what we want.

Thanks for any help


Response to Nathanael

Your comments are exactly correct.

  1. I expected http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray to work exactly as it does

  2. Yes, our problem is that the image heights are not known beforehand, but it's good to know that this works with a known height

  3. I think it would be great if there was a command for scale=padwidth that would work with variable heights. Or a setting for padwidth=true and padheight=false that could be used in conjunction with scale=canvas.


Solution

  • So, let's say that you're given an 800x600px image, and you apply ?width=1000&scale=canvas. You were expecting that this would produce a 1000x600px image, but instead it produced a 1000x750px image, right?

    http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray

    If you specify the height explicitly, the padding goes away - but you may not know the image height beforehand, correct?

    http://z.zr.io/ri/red-leaf.jpg?width=1000&scale=canvas&bgcolor=gray&height=600

    What would be the least surprising behavior - maintaining aspect ratio, or only adding the minimum padding required? How would you expect this to behave, or be exposed as a command?