Click to See Complete Forum and Search --> : Perl & MySQL - Handling Images


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

A_Tame_Lion
04-26-2011, 09:07 AM
Realised the return variable was wrong, but changed that, still the same result.

Sixtease
04-27-2011, 02:45 AM
I think given the features of your webapp, it would pay off to switch to Catalyst. You have everything canned: file uploads, sessions, login, everything.

A_Tame_Lion
04-29-2011, 09:14 AM
I think given the features of your webapp, it would pay off to switch to Catalyst. You have everything canned: file uploads, sessions, login, everything.

Hi sixtease,

Just got back from a few days work in my other job. The idea behind catalyst sounds very logical and sensible, I have done a little reading up on it and am going to look seriously into this.

One problem I have installing stuff from CPAN is that it often fails on prerequisits and tells me a make install seems impssible or some such message.

From command line I am generally running:

perl -MCPAN -eshell

install Task::Catalyst

Another common error I get is one about an error reading the FTPstats.yml file, and an error trying to 'parse' YAML file. With YAML:XS the following error was encountered: Usage: YAML::XS::LibYAML::Load(yaml_sv) at C:\Perl\lib/YAML/XS.pm line 48

Sometimes if I retry a few times, deleting the FTPstats.yml file or restarting the session so the lockfile is removed, I might get something to install. Any ideas on that? Or a way to make CPAN install prerequisites as well as whatever it is I actually want to install?

Sixtease
04-29-2011, 09:22 AM
Hey there,

if you have trouble using CPAN to install Catalyst (it always worked for me on Ubuntu), then I suggest to use a pre-packaged install method for your operating system. I vaguely remember you mentioned using Windows? Are you using Strawberry perl? Or ActivePerl?

A_Tame_Lion
04-29-2011, 10:07 AM
Forgot to mentioned I am indeed running Windows and using Active Perl, can't find Catalyst in PPM though.

Sixtease
04-29-2011, 10:26 AM
I have zero experience installing Catalyst on ActiveState perl (and may God help me keep it that way) but this looks to be the most reliable info on how to tackle it.

http://wiki.catalystframework.org/wiki/installingcatalyst#MS_Windows_-_ActiveState_perl_.28.2A.ppm.29

A_Tame_Lion
05-01-2011, 05:52 AM
Thanks Sixtease, I know Windows can be a pain - didn't realise there were other distributions of Perl, and yes it has been difficult to install Cataylist (5.80032) on this platform (the method you suggested didn't work - site appears to be offline/defunct - but I muddled through by manually installing CPAN dependencies, had to force install a few modules, and also installed Dev-C++ etc). I know a little linux/unix but development resources have me tied to the Windows platform at the moment (linux server OS are incompatible with certain components of the server I have - quite an old one). Will see if I can re-work my existing code to fit with the Catalyst installation.

A_Tame_Lion
05-02-2011, 03:48 AM
Just a quick query though, what version of Perl are you using? What do you find are the benefits of using this version?

Sixtease
05-03-2011, 02:57 AM
I use 5.10.1. The only benefit over using a newer stable version is that it comes with my OS (ubuntu) and I don't have to bother about anything.