A_Tame_Lion
04-26-2011, 08:04 AM
Hi,
I have been researching on the net to find out about using Perl to accept images perhaps input via a html forms file upload field, process them and write them to a MySQL database. I had heard that Image::Magick is difficult to install (I also tried and had errors with CPAN install), and I then read about GD which happens to be installed on my system already, so I have decided to use this for now, but I am having problems uploading images. My script was worked fine (with text based data) until I tried to introduce this functionality, now I am getting the error:
"CGI Error - The specified CGI application misbehaved by not returning a complete set of HTTP headers."
What I am ultimately trying to do is upload pictures submitted through a html forms file upload field, check that the file extensions match .jpg, .jpeg, .gif, or .png, resize the image to say 100x100px, name the image according to some variables on the form, save the image in a specified folder, and write a reference it to the image files location in a MySQL database - as I have heard it isn't a good idea to store images in a database because of performance/security issues.
My code thus far is:
main script call:
$ucPicture = images::imageHandler($ucPicture)
images module code (function is definitely exported):
sub imageHandler {
my $normImage ="";
my $white = "";
my ($image_ref) = @_;
if ( $image_ref->[0] =~ /([\w.]+\.jpg)$/i || $image_ref->[0] =~ /([\w.]+\.jpeg)$/i) {
open (IMAGE, $image_ref->[0]) || die;
$normImage = newFromJpeg GD::Image(100,100, \*IMAGE) || die;
$white = $normImage->colorClosest(255,255,255);
$normImage->transparent($white);
$normImage->interlaced('true');
binmode STDOUT;
$normImage->png;
$imageName = ($main::ucUserName . "\.png");
$imageName = $normImage;
close IMAGE;
}
elsif ( $image_ref->[0] =~ /([\w.]+\.png)$/i ) {
open (IMAGE, $image_ref->[0]) || die;
$normImage = newFromPng GD::Image(100,100, \*IMAGE) || die;
$white = $normImage->colorClosest(255,255,255);
$normImage->transparent($white);
$normImage->interlaced('true');
binmode STDOUT;
$normImage->png;
$imageName = ($main::ucUserName . "\.png");
$imageName = $normImage;
close IMAGE;
}
elsif ( $image_ref->[0] =~ /([\w.]+\.gif)$/i ) {
open (IMAGE, $image_ref->[0]) || die;
$normImage = newFromGif GD::Image(100,100, \*IMAGE) || die;
$white = $normImage->colorClosest(255,255,255);
$normImage->transparent($white);
$normImage->interlaced('true');
binmode STDOUT;
$normImage->png;
$imageName = ($main::ucUserName . "\.png");
$imageName = $normImage;
close IMAGE;
}
else {
print "You have selected an image type this application does not recognise. Please try again.";
}
return $normImage;
}
Any suggestions for how I can progress this script, or maybe if there is another Perl module I could consider using?
TIA
I have been researching on the net to find out about using Perl to accept images perhaps input via a html forms file upload field, process them and write them to a MySQL database. I had heard that Image::Magick is difficult to install (I also tried and had errors with CPAN install), and I then read about GD which happens to be installed on my system already, so I have decided to use this for now, but I am having problems uploading images. My script was worked fine (with text based data) until I tried to introduce this functionality, now I am getting the error:
"CGI Error - The specified CGI application misbehaved by not returning a complete set of HTTP headers."
What I am ultimately trying to do is upload pictures submitted through a html forms file upload field, check that the file extensions match .jpg, .jpeg, .gif, or .png, resize the image to say 100x100px, name the image according to some variables on the form, save the image in a specified folder, and write a reference it to the image files location in a MySQL database - as I have heard it isn't a good idea to store images in a database because of performance/security issues.
My code thus far is:
main script call:
$ucPicture = images::imageHandler($ucPicture)
images module code (function is definitely exported):
sub imageHandler {
my $normImage ="";
my $white = "";
my ($image_ref) = @_;
if ( $image_ref->[0] =~ /([\w.]+\.jpg)$/i || $image_ref->[0] =~ /([\w.]+\.jpeg)$/i) {
open (IMAGE, $image_ref->[0]) || die;
$normImage = newFromJpeg GD::Image(100,100, \*IMAGE) || die;
$white = $normImage->colorClosest(255,255,255);
$normImage->transparent($white);
$normImage->interlaced('true');
binmode STDOUT;
$normImage->png;
$imageName = ($main::ucUserName . "\.png");
$imageName = $normImage;
close IMAGE;
}
elsif ( $image_ref->[0] =~ /([\w.]+\.png)$/i ) {
open (IMAGE, $image_ref->[0]) || die;
$normImage = newFromPng GD::Image(100,100, \*IMAGE) || die;
$white = $normImage->colorClosest(255,255,255);
$normImage->transparent($white);
$normImage->interlaced('true');
binmode STDOUT;
$normImage->png;
$imageName = ($main::ucUserName . "\.png");
$imageName = $normImage;
close IMAGE;
}
elsif ( $image_ref->[0] =~ /([\w.]+\.gif)$/i ) {
open (IMAGE, $image_ref->[0]) || die;
$normImage = newFromGif GD::Image(100,100, \*IMAGE) || die;
$white = $normImage->colorClosest(255,255,255);
$normImage->transparent($white);
$normImage->interlaced('true');
binmode STDOUT;
$normImage->png;
$imageName = ($main::ucUserName . "\.png");
$imageName = $normImage;
close IMAGE;
}
else {
print "You have selected an image type this application does not recognise. Please try again.";
}
return $normImage;
}
Any suggestions for how I can progress this script, or maybe if there is another Perl module I could consider using?
TIA