title: ImageMagick: Change aspect ratio of image without cropping and within a max width and height
body: <p><em>edit: End goal is a 2:1 aspect ratio, no cropping, and a max size of 1260x630. If the image dimensions are originally below 1260x630, they should stay below 1260x630.</em></p>
  
  <p>Struggling to word this well (which very well may be the reason I haven't found a good answer).</p>
  
  <p>With ImageMagick I'm trying to resize an image down to 1260x630 provided the image itself is larger than the given size (width OR height). </p>
  
  <p>Once resized I need to change the aspect ratio to a 2:1 without cutting off the image. I've accomplished this well enough with larger images using the following:</p>
  
  <pre><code>convert foobar-original.jpg \
     -resize 1260x630\&gt;       \
     -gravity center          \
     -extent 1260x630         \
      foobar.jpg
  </code></pre>
  
  <p>A 1500x500 picture, for example, using the above, becomes first 1260x420 (with the resize) and then 1260x630 (via the extent), and the extra width is filled in with white background. </p>
  
  <p>However, if the image starts off smaller, 800x200 for example, the image is <em>not</em> resized (as expected) but then is made to fit within a full 1260x630. This is not the desired result. </p>
  
  <p>I would rather <code>-extent</code> simply affect the aspect ratio, adding on white as needed. So rather than the 800x200 image converting to 1260x630, with the 800x200 part of the image in the center, I would like the image to be converted to 800x400 (that 2:1 ratio), the extra height filled in with white space. </p>
  
  <p>Basically, I'm looking for a way to set <code>-extent</code> to an aspect ratio, rather than a specific size or percentage. Something like</p>
  
  <pre><code>convert foobar-original.jpg \
     -resize 1260x630\&gt;       \
     -gravity center          \
     -extent 2:1              \
      foobar.jpg
  </code></pre>