$w_scale = ($in{w} / $width); $h_scale = ($in{h} / $height); if ($w_scale > $h_scale) { $tn_width = $width * $h_scale; $tn_height = $height * $h_scale; } else { $tn_width = $width * $w_scale; $tn_height = $height * $w_scale; } $tn = GD::Image->new($tn_width, $tn_height, 1); $tn->copyResized($image,0,0,0,0,$tn_width,$tn_height,$width,$height);