Imagemagick Crop Team Does Not Perfect

I use imagemagick to resize and crop the image.

Test image:

enter image description here

I need to resize for a 300 x 320 frame for this, first I resize the image and then crop it and I use the following commands:

exec("convert /uploadImagePath -thumbnail 300 /newImagePath"); exec("convert /newImagePath -gravity Center -crop 290x310+0+0 /newImagePath"); 

But it gives me the following image

enter image description here

As you can see, the image is not complete ... please help me where I am wrong

+7
php imagemagick
Jul 30 2018-12-12T00:
source share
2 answers

Try:

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

Hope that helps

+6
Jul 30 2018-12-12T00:
source share

(Answer updated, now an example is shown for -liquid-rescale below)




Your original image sizes:

 489 x 640 pixels 

Your desired sizes are as follows:

 290 x 310 pixels 

It cannot scale to these sizes without:

  • cropping (do not save all areas of the original image)
  • preservation of the desired width (give the desired height)
  • maintaining the desired height (discard the required width)
  • distortion (do not keep aspect ratio when scaling)
  • padding (add multiple pixels to one or more edges)
  • removing pixels where it’s not obvious (“fluid scaling” or “cutting seams” - see Wikipedia )

Your result shows '1.' (cropping) what you don't like. So you have the option "2." (holding the width), '3.' (storage height), '4.' (distortion), '5.' (indentation) and "6." (seam thread) to be checked.

'2.': Preservation of the desired height

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

The resulting image is 237 x 310 pixels .
Keep Height.... Keep height
(automatically detect the width)

'3.': Preservation of the desired width

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

The resulting image is 290 x 380 pixels .
Keep Width..... Keep width
(automatically detect height)

'4.': Distortion

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

The resulting image is 290 x 310 pixels .
Distorted...... Distorted
(ignore aspect ratio - distort the image, if necessary, to fit the size)

'5.': Padding

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

The resulting image is 290 x 310 pixels . (An orange background was added only to demonstrate that the “extension” of the image really worked.)
Padded......... Padded
(keep proportions - enlarge image for desired sizes)

'6.': Thread thread

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

Strike> The above would be a command that you spontaneously derive from quickly reading out the link for ImageMagick command parameters. However, this will not work, 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 

The following explains why I needed to modify it ....
Liquid-rescaled 'Liquidly' rescaled
Sorry - I can’t imagine a rough image right now; this requires an additional delegate ImageMagick liblqr (liquid scaling library), which I do not have at the moment) Now I have the opportunity to create a "liquid scaled" version of the original image.




'-liquid-rescale' / '-liquid-rescale' :

As stated above, the last image is not the result of my originally proposed command, but 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 that we have an original image of 489x610 pixels, which is expected to scale to 290x310 pixels. But -liquid-rescale not suitable for scaling in two dimensions at the same time - it is intended for scaling in only one direction (horizontal or vertical). If you try to do both at once, the results may not be what you expected. Here is the result for the originally proposed command:

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

LQR gone wrong liquid-rescale gone wrong

This is why I came up with two modified commands that work in two stages:

  • First apply fluid scaling to the horizontal size only, expanding the original width from 489 pixels to 599 pixels.
  • Second, apply the “normal” aspect ratio scaling to the intermediate result to get the final image.
+44
Jul 30 '12 at 12:14
source share



All Articles