my ($width, $height ) = ...; my ($max_width, $max_height ) = (800, 600); my ($width_factor, $height_factor) = ($max_width/$width, $max_height/$height); if ($width_factor < 1 || $height_factor < 1) { my $factor = ($width_factor < $height_factor ? $width_factor : $height_factor ); $width = int($width * $factor + 0.5); $height = int($height * $factor + 0.5); }