PHP Imagick: how to resize without increasing?

I am using PHP Imagick to manipulate images.

I have two use cases: "resize" and "crop".

In "resizing", if the original dimensions of the image are less than the given width and height, I don't want Imagick to scale it and instead want it to just revert to its original size.

However, as the PHP documentation says, Imagick's behavior was changed from version 3 (I'm using version 6+) so that it would always enlarge the image.

Note. The bestfit parameter behavior has changed in Imagick 3.0.0. Before this version, given the dimensions 400x400, the image with dimensions 200x150 will remain intact. Imagick 3.0.0 and later will scale to 400x300 as this is the "best fit" for the given dimensions. If the bestfit parameter is used, both width and height must be specified.

So in my case, for a given dimensions of 400x400, a 200x150 image should return a 200x150 image, not a 400x300 one. For the given dimensions 100x100, a 200x150 image should return a 100x75 image (that is, scaling should occur).

I tried thumbnailImage (), resizeImage () and scaleImage () with no luck. If I set bestfit to false it does a crop, which is not what I want with the resize.

Is there a way to do this with Imagick?

+3


source to share


2 answers


Try it like this:



$max_width  = 1200;
$max_height = 800;
$im = new Imagick($path_to_image);
$im->resizeImage(
    min($im->getImageWidth(),  $max_width),
    min($im->getImageHeight(), $max_height),
    imagick::FILTER_CATROM,
    1,
    true
);

      

+2


source


Use this command:

convert input.img -resize 400x400\> output.img

      



Note the specific way to provide the required dimension with the added modifier \>

: it tells the transform process to only resize images that are larger than 400x400 pixels. Images that are smaller are not affected.

-1


source







All Articles