Imagemagick Crop command not giving perfect result

(Answer is updated, providing an illustrated example for -liquid-rescale further below now)


Your original image's dimensions are:

489 x 640 pixels

Your desired dimensions seem to be:

290 x 310 pixels

This cannot scale to these dimensions without either:

  1. cropping (do not keep all areas of the intial image)
  2. keeping desired width (give up desired height)
  3. keeping desired height (give up desired width)
  4. distortion (do not keep the aspect ratio when scaling)
  5. padding (add more pixels to one or more edges)
  6. removing pixels where it's not obvious ("liquid rescale" or "seam carving" -- see Wikipedia)

Your result shows '1.' (cropping), which you don't like. So you have options '2.' (keeping width), '3.' (keeping height), '4.' (distortion), '5.' (padding) and '6.' (seam carving) left to test.

'2.': Keeping desired Height

convert WPTgp.jpg -resize x310 keep-height.jpg

Resulting Image has dimensions of 237 x 310 pixels.
Keep Height.... Keep Height
(determine width automatically)

'3.': Keeping desired Width

convert WPTgp.jpg -resize 290x keep-width.jpg

Resulting Image has dimensions of 290 x 380 pixels.
Keep Width..... Keep Width
(determine height automatically)

'4.': Distortion

convert WPTgp.jpg -resize 290x310\! distorted.jpg

Resulting Image has dimensions of 290 x 310 pixels.
Distorted...... Distorted
(ignore aspect ratio -- distort image if required to fit dimensions)

'5.': Padding

convert WPTgp.jpg \
   -resize 290x310 \
   -gravity center \
   -background orange \
   -extent 290x310 \
    padded.jpg

Resulting Image has dimensions of 290 x 310 pixels. (Orange background was added only to demonstrate that the 'extention' of the image did work.)
Padded......... Padded
(keep aspect ratio -- extend image for desired dimensions)

'6.': Seam Carving

convert WPTgp.jpg -liquid-rescale 290x310\! liquid.jpg

The above would be the command you'd spontaneously derive from quick-reading the ImageMagick command options reference. However, it doesn't work well, and instead I used:

convert WPTgp.jpg -liquid-rescale 599x640\! -scale 290x310 liquid.jpg
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 48.4%   liquid.jpg

Further below is an explanation why I needed to modify it....
Liquid-rescaled 'Liquidly' rescaled
Sorry -- I cannot provide example picture right now; this requires the additional ImageMagick delegate liblqr (liquid rescaling library) to be installed, which I don't have at this moment) I've now had the opportunity to create a 'liquidly rescaled' version of the original image.


Caveats about Seam Carving / '-liquid-rescale':

As stated above, the last image is not the result of my originally proposed command, but of one of these two modified versions:

convert WPTgp.jpg -liquid-rescale 599x640\! -scale 290x310 liquid.jpg
convert WPTgp.jpg -liquid-rescale 599x640\! -scale 48.4%   liquid.jpg

Remember, we have an original image of 489x610 pixels, which we are expected to scale to 290x310 pixels. But -liquid-rescale isn't good at rescaling in two dimensions at once -- it's designed to scale into one direction only (horizontal or vertical). If you try to do both at once, results may not be what you'd expect. Here is the result for the originally proposed command:

 convert WPTgp.jpg -liquid-rescale 290x310\! liquid.jpg

LQR gone wrong liquid-rescale gone wrong

That's why I came up with the two modified commands which work in two steps:

  1. First, apply liquid rescaling to the horizontal dimension only, expanding the original's width from 489 pixels to 599 pixels.
  2. Second, apply 'normal' aspect-ratio-keeping scaling to the intermediate result to produce the final image.

Try:

$inputFile = "WPTgp.jpg";
exec("convert {$inputFile} -resize 290x310^ -gravity Center -crop 290x310+0+0 picCropped.png");

Hope that helps