$ratio = 1; if ($curwidth > $maxwidth) $ratio = $maxwidth / $curwidth; if ($curheight > $maxheight) { $hratio = $maxheight / $curheight; if ($hratio < $ratio) $ratio = $hratio; } $maxwidth = $curwidth * $ratio; $maxheight = $curheight * $ratio;