use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use GD; sub thumbnail { use File::Copy; $maxThumbSize = "150"; $maxImageSize = "600"; ($imageBase,$crap) = split(/\./,$name); $imageBase =~ s/^\s+//g; $imageBase =~ s/\s+$//g; $imageBase =~ s/\"//g; $imageBase =~ s/-//g; $imageBase =~ s/ //g; $imageURL = $albumFolderURL . "/" . $category . "/" . $album . "/Images/" . $name; $imagePath = $imageFolder . $S . $name; $categoryCoverPath = $albumFolderPath . $S . $category . $S . "cover.jpg"; $albumCoverPath = $albumFolderPath . $S . $category . $S . $album . $S . "cover.jpg"; if ($name =~ m/gif$/) { &error('GIF files are copyrighted through 2004'); } if (($name =~ m/jpg$/i) || ($file =~ m/jpeg$/i)) { $sourceImage = GD::Image->newFromJpeg($imagePath); $retype = "no"; } elsif ($name =~ m/png$/i) { open (PNG,imagePath) || &error('Cannot Open PNG File'); $sourceImage = newFromPng GD::Image(PNG) || &error('Cannot Read From PNG File'); close PNG; $retype = "yes"; } elsif ($name =~ m/bmp$/i) { $sourceImage = GD::Image->newFromWMP("$imagePath"); $retype = "yes"; } ($uploadWidth,$uploadHeight) = $sourceImage->getBounds(); # Resize Original if too big ############################# if ((($uploadWidth > $maxImageSize) || ($uploadHeight > $maxImageSize)) || ($retype eq "yes")) { if ($uploadWidth > $uploadHeight) { $scalefactor=$maxImageSize/$uploadWidth; } else { $scalefactor=$maxImageSize/$uploadHeight; } $fileWidth = int($uploadWidth*$scalefactor); $fileHeight = int($uploadHeight*$scalefactor); $resizedOriginal = new GD::Image($fileWidth, $fileHeight); $resizedOriginal->copyResampled($sourceImage,0,0,0,0,$fileWidth,$fileHeight,$uploadWidth,$uploadHeight); $newImagePath = $imageFolder . $S . $imageBase . ".jpg"; unlink("$imagePath"); open(IMAGE, ">$newImagePath") or &error('Cannot Open New File'); if ($opsys eq "dos") { binmode IMAGE; } else { flock(IMAGE, 2); } print IMAGE $resizedOriginal->jpeg(75); close(IMAGE) or &error('Cannot Close New File'); chmod(0644, $newImagePath); } # else tranfer images size to new variables else { $fileWidth=$uploadWidth; $fileHeight=$uploadHeight; $newImagePath = $imagePath; } # Create Thumbnail if necessary ############################### if ((($fileWidth gt $maxThumbSize) || ($fileHeight gt $maxThumbSize)) || ($retype eq "yes")) { if ($fileWidth gt $fileHeight) { $scalefactor=$maxThumbSize/$fileWidth; } else { $scalefactor=$maxThumbSize/$fileHeight; } $thumbWidth = int($fileWidth*$scalefactor); $thumbHeight = int($fileHeight*$scalefactor); } else { $thumbWidth=$fileWidth; $thumbHeight=$fileHeight; } print "Make Thumbnail, FW = $fileWidth, FH = $fileHeight, TW = $thumbWidth, TH = $thumbHeight
"; $thumbnailImage = new GD::Image($thumbWidth, $thumbHeight); $thumbnailImage->copyResampled($sourceImage,0,0,0,0,$thumbWidth,$thumbHeight,$fileWidth,$fileHeight); $thumbPath = $thumbFolder . $S . $imageBase . "-" . $fileWidth . "x" . $fileHeight . ".jpg"; if (-f $thumbPath) { unlink("$thumbPath"); } open(THUMB, ">$thumbPath") or &error('Cannot Open New Thumbnail'); if ($opsys eq "dos") { binmode THUMB; } else { flock(THUMB, 2); } if ((($fileWidth gt $maxThumbSize) || ($fileHeight gt $maxThumbSize)) || ($retype eq "yes")) { print THUMB $thumbnailImage->jpeg(75); } else { print THUMB $sourceImage->jpeg(75); } close(THUMB) or &error('Cannot Close New Thumbnail'); chmod(0644, $thumbPath); }