I am using Imagemagick for resizing and cropping image.
Test Image :
I need to re-size it for 300 x 320 frame for this first I am resizing the image and then cropping it and i am using the following commands:
exec("convert /uploadImagePath -thumbnail 300 /newImagePath");
exec("convert /newImagePath -gravity Center -crop 290x310+0+0 /newImagePath");
But it gives me following image
As you can see image is not complete. Where am I mistaken?
(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:
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.
convert WPTgp.jpg -resize x310 keep-height.jpg
Resulting Image has dimensions of 237 x 310 pixels
.Keep Height....
(determine width automatically)
convert WPTgp.jpg -resize 290x keep-width.jpg
Resulting Image has dimensions of 290 x 380 pixels
.Keep Width.....
(determine height automatically)
convert WPTgp.jpg -resize 290x310\! distorted.jpg
Resulting Image has dimensions of 290 x 310 pixels
.Distorted......
(ignore aspect ratio -- distort image if required to fit dimensions)
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.........
(keep aspect ratio -- extend image for desired dimensions)
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
Sorry -- I cannot provide example picture right now; this requires the additional ImageMagick delegate I've now had the opportunity to create a 'liquidly rescaled' version of the original image.liblqr
(liquid rescaling library) to be installed, which I don't have at this moment)
'-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
That's why I came up with the two modified commands which work in two steps:
Try:
$inputFile = "WPTgp.jpg";
exec("convert {$inputFile} -resize 290x310^ -gravity Center -crop 290x310+0+0 picCropped.png");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With