I don't use Imagemagick very often, which is why I don't know how to solve this problem. I also don't know how to phrase the question other than: How do I get the image to be cropped like the CSS property: background-size: contain; I haven't been able to find an answer, likely due to my phrasing.
I need the image to be 200px, and "resized/cropped" so that it is not stretched, but contained by the width or height (dependent on the orientation of the image width > height = contain by width
)
What I have so far:
$im = new Imagick($path);
/* Resizing Operations */
$gm = $im->getImageGeometry();
$w = $gm['width'];
$h = $gm['height'];
if($h < $w) {
$nh = 200;
$nw = (200 / $h) * $w;
} else {
$nw = 200;
$nh = (200 / $w) * $h;
}
$im->resizeImage($nw, $nh, Imagick::FILTER_LANCZOS, true);
$im->cropThumbnailImage(200,200);
/* End Resizing Operations */
Which produces an image with the center chopped out.
Here's a visual example
We have this logo:
And then we want it to be constrained to 200px wide and 200px high, contained:
Essentially like setting the canvas height, while not adjusting the image height.