Update: Sorry - I was half asleep and did not read the question properly. I'll leave the code all the same
Old and untidy code :( I've trimmed out some parts (checking if file of same name already exists, for example)
sub upload_file {
my $upfilename = $q->param('filebrowser');
$upfilename = (split(/\\/, $upfilename))[-1];
my $upfile = $q->upload('filebrowser');
if (!$upfile && $q->cgi_error) {
print $q->header(-status=>$q->cgi_error);
} else {
$filepath = "$images_directory/$upfilename";
open (NEWFILE, ">$filepath") or &error_code("Could not save to
+ $filepath");
binmode NEWFILE;
my ($mbytesread, $mbuffer, $mBytes);
while ($mbytesread = read($upfile, $mbuffer, 1024)) {
$mBytes += $mbytesread;
print NEWFILE $mbuffer;
}
close(NEWFILE);
if ($mBytes <= 0) {
&error_code("Uploaded file is 0 bytes in size and will be
+discarded.");
unlink $filepath;
}
# create thumbnail
use GD;
use Image::GD::Thumbnail;
# Load your source image
open (IN, "<$filepath") or &error_code("Could not open $filepa
+th to create thumbnail");
my $srcImage = GD::Image->newFromJpeg(*IN);
close(IN);
# Create the thumbnail from it, where the biggest side is 200
+px
my ($thumb,$x,$y) = Image::GD::Thumbnail::create($srcImage,200
+);
# Save your thumbnail
open (OUT, ">$thumbs_directory/$upfilename") or &error_code("C
+ould not save to $thumbs_directory/$upfilename");
binmode OUT;
print OUT $thumb->jpeg;
close(OUT);
}