Came up with this algorithm based on the extent method of ImageMagick, which achieves the same result as CSS's background-size: contain;
You can set the 200
value in the resizeImage function to get your end product. Works beautifully!
$im = new Imagick($path);
/* Resizing Operations */
$gm = $im->getImageGeometry();
$w = $gm['width'];
$h = $gm['height'];
if($h < $w) {
$sr = $w;
$horz = TRUE;
} else if($h > $w) {
$sr = $h;
$horz = FALSE;
} else {
$square = TRUE;
}
if(!$square && $horz) {
$srs = $sr / 2;
$extent_amt = $srs - ($h / 2);
$im->extentImage($sr, $sr, 0, 0 - $extent_amt);
} else if(!$square && !$horz) {
$srs = $sr / 2;
$extent_amt = $srs - ($w / 2);
$im->extentImage($sr, $sr, 0 - $extent_amt, 0);
}
$im->resizeImage(200, 200, Imagick::FILTER_LANCZOS, true);
/* End Resizing Operations */
$im->writeImage($path);
/* Clean up time */
$im->clear();
$im->destroy();