my $srcimage = GD::Image->newFromJpeg($target_dir."/".$newName); my ($srcW,$srcH) = $srcimage->getBounds(); if( $srcW*$srcH > 4000000 ){ my $newimage = new GD::Image(100,100); $newimage->gdImageCreateFromGd2Part($srcimage,100,100,100,100); # future code to make a thumbnail from the part that is pulled from the image }else{ my $maxheight = 400; my $maxwidth = 400; my $wdiff = $srcW - $maxwidth; my $hdiff = $srcH - $maxheight; my $newH; my $newW; if ($wdiff > $hdiff){ $newW = $maxwidth; $aspect = ($newW/$srcW); $newH = int($srcH * $aspect); }else{ $newH = $maxheight; $aspect = ($newH/$srcH); $newW = int($srcW * $aspect); } my $newimage = new GD::Image($newW,$newH); $newimage->copyResized($srcimage,0,0,0,0,$newW,$newH,$srcW,$srcH); open(FILE, ">".$target_dir."/preview/".$newName) || die "Cannot open image: $!\n"; print FILE $newimage->jpeg; }