Here is a function to calculate the new dimensions of a thumbnail, to fit within the given dimensions on both sides.
<?php
function scaleImage($x,$y,$cx,$cy) {
list($nx,$ny)=array($x,$y);
if ($x>=$cx || $y>=$cx) {
if ($x>0) $rx=$cx/$x;
if ($y>0) $ry=$cy/$y;
if ($rx>$ry) {
$r=$ry;
} else {
$r=$rx;
}
$nx=intval($x*$r);
$ny=intval($y*$r);
}
return array($nx,$ny);
}
?>
Use it like this:
<?php
$thumb=new Imagick($originalImageFilename);
list($newX,$newY)=scaleImage(
$thumb->getImageWidth(),
$thumb->getImageHeight(),
$newMaximumWidth,
$newMaximumHeight);
$thumb->thumbnailImage($newX,$newY);
$thumb->writeImage($thumbnailFilename);
?>