# Check if we are limited by height or width of the source image my $factor = $sourcewidth / $targetwidth; if($sourceheight / $factor > $targetheight) { $factor = $sourceheight / $targetheight; ) my $newwidth = int($sourcewidth / $factor); my $newheight = int($sourceheight / $factor);