Uploading images via HTML / PerlMagick
Posted: 2007-03-30T12:34:54-07:00
Greetings,
We were using an older version of ImageMagick / PerlMagick on a Red Hat 7.3 server until last week when it went kaputski. I set up an Edgy (Ubuntu 6.10) server with LAMP, added mod_perl and the latest imageMagick / PerlMagick updates (6.3.3-4). It may be worth noting that I'm a beginner when it comes to Linux ops, so bear with me...
The site is primarily HTML and PERL scripts that puts together timecard forms with user data and images (where ImageMagick comes in). The idea is for the user to upload whatever image they want and the site resizes and dumps it in a shared folder that the images are then drawn from later when a user views employee information.
The site's functionality works fine, save for the upload script that allows a user to find an image on their desktop and upload it to the server. The script below parses fine, but the image is not actually uploaded. If I manually copy an image into the server's folder with the proper name (employee number + .jpg), it displays fine, however. If you try to upload something when an image for that employee is already present, the old photo does not change. Nor does it place a new photo if one is not already present.
The code did work under the previous server.
The upload script is as follows:
***************************************
#!/usr/bin/perl
use DB_File;
use Image::Magick;
sub endit
{
$_[0] =~ s/^.*\=\"//;
$_[0] =~ s/\".*$//;
}
print "Content-type: text/html\n\n";
open (FILE, "-");
$boundary = <FILE>;
chomp ($boundary);
$blength = length ($boundary);
$junk = <FILE>;
$junk = <FILE>;
$sess = <FILE>;
chomp $sess;
$sess =~ s/\s//g;
$junk = <FILE>;
$junk = <FILE>;
$junk = <FILE>;
$empnum = <FILE>;
chomp $empnum;
$empnum =~ s/\s//g;
$junk = <FILE>;
$junk = <FILE>;
$junk = <FILE>;
$rotate = <FILE>;
chomp $rotate;
$rotate =~ s/\s//g;
$junk = <FILE>;
$filename = <FILE>;
endit ($filename);
$type = <FILE>;
chomp $type;
$junk = <FILE>;
$ext = "";
if ($filename =~ /\.jpg/i) { $ext = ".jpg"; }
if ($filename =~ /\.jpeg/i) { $ext = ".jpg"; }
if ($filename =~ /\.gif/i) { $ext = ".gif"; }
if ($filename =~ /\.png/i) { $ext = ".png"; }
if ($type =~ /jpg/i) { $ext = ".jpg"; }
if ($type =~ /jpeg/i) { $ext = ".jpg"; }
if ($type =~ /gif/i) { $ext = ".gif"; }
if ($type =~ /png/i) { $ext = ".png"; }
$temp = sprintf("upload%5d", $$);
$temp =~ s/\s/0/g;
$name = $temp . $ext;
$jname = $empnum . ".jpg";
$thumb = $empnum . "t.jpg";
unlink "photos/$name";
$size = 0;
if ($ext ne "")
{
open (IMAGE, ">photos/$name");
while (<FILE>)
{
if (substr ($_, 0, $blength) eq $boundary) { last; }
print IMAGE "$_";
$size = $size + length ($_);
}
close (IMAGE);
$t = chmod 0660, "photos/$name";
}
close (FILE);
$image = Image::Magick->new;
$image->Read("photos/$name");
unlink "photos/$name";
if ($rotate =~ /l/i)
{
$image->Set(bordercolor=>'#ffffff');
$image->Rotate(degrees=>270, crop=>0, sharpen=>0);
}
if ($rotate =~ /r/i)
{
$image->Set(bordercolor=>'#ffffff');
$image->Rotate(degrees=>90, crop=>0, sharpen=>0);
}
$image->Crop('0x0');
($h, $w, $d) = $image->Get('rows', 'columns', 'depth');
if ($w > ($h * 0.75))
{
$tw = ($w - $h * 0.75) / 2;
$w = $h * 0.75;
$image->Crop(width=>$w, height=>$h, x=>$tw, y=>0);
}
if ($h > ($w * 1.333))
{
$th = ($h - $w * 1.333) / 2;
$h = $w * 1.333;
$image->Crop(width=>$w, height=>$h, x=>0, y=>$th);
}
if ($h > 640)
{
$h = 640;
$w = 480;
$scale = "$w" . "x" . "$h";
$image->Scale($scale);
}
$image->Write("jpeg:photos/$jname");
if ($h > 160)
{
$h = 160;
$w = 120;
$scale = "$w" . "x" . "$h";
$image->Scale($scale);
$image->Sharpen('50');
}
$image->Set(quality=>50);
$image->Write("jpeg:photos/$thumb");
undef $image;
$t = chmod 0660, "$imageloc/upload/$jname";
$t = chmod 0660, "$imageloc/upload/$thumb";
print "
<html>
<head>
<title>Human resources - display employee info</title>
</head>
<body bgcolor=white background=/images/aslogob1.jpg>
<table width=100% cols=1><tr><td bgcolor=$color>
<font size=+4 color=white>
<i> Audiosears Corporation</i>
</font>
<hr>
<img src=fetchimg?images/aslogwt.gif align=right>
<font size=+2 color=white>
<b> Human Resources</b><br>
<b> Display Employee Information</b>
</font>
<br></table><br>
";
if ($empnum > 0)
{
tie %employee, 'DB_File', "employee.db";
@fields = split (/\|/, $employee{$empnum});
untie %employee;
print "
<img src=fetchimg?photos/$empnum.jpg align=left height=320 width=240>
<table cols=2 width=350 border=0>
<tr valign=top>
<td align=right><font size=+1>Number:
<td><font size=+1>$empnum
<tr valign=top>
<td align=right><font size=+1>Name:
<td><font size=+1>$fields[1] $fields[2] $fields[0]
<tr valign=top>
<td align=right><font size=+1>Status:
<td><font size=+1>$fields[3]
<tr valign=top>
<td align=right><font size=+1>Department:
<td><font size=+1>$fields[4]
<tr valign=top>
<td align=right><font size=+1>Title:
<td><font size=+1>$fields[5]
<tr valign=top>
<td align=right><font size=+1>Class:
<td><font size=+1>$fields[6]
<tr valign=top>
<td align=right><font size=+1>Location:
<td><font size=+1>$fields[7]
</table>
<br clear=all><br><hr><br>
";
}
print "
<table cols=2 width=2 border=0><tr valign=top>
<td width=1>
<form method=post action=timecard>
<input type=hidden name=sess value=\"$sess\">
<input type=hidden name=num1 value=$empnum>
<input type=submit value=\"Print Timecard\">
</form>
<td width=1>
<form method=post action=editinfo>
<input type=hidden name=sess value=\"$sess\">
<input type=hidden name=empnum value=$empnum>
<input type=submit value=\"Edit Employee\">
</form>
</table>
<form method=post action=empinfo>
<input type=hidden name=sess value=\"$sess\">
<input type=text name=empnum value=\"\" size=6 maxlength=6>
<input type=submit value=\"View Employee\">
</form>
<form method=post action=emplookup>
<input type=hidden name=sess value=\"$sess\">
<input type=text name=search value=\"\" size=20 maxlength=20>
<input type=submit value=\"Search\">
</form>
</body>
</html>
";
**************************************************
The 'photos' folder is located at /server/humanres/photos - the script above and all other server scripts are in the humanres folder and executable by Apache. So far, I can only think of the following as issues:
1) Permissions issue in photos folder for posting uploads? Right now permissions on it are wide open (for testing on localhost) - should that folder be owned by a specific user or group?.
2) Version conflicts of ImageMagick - this code was written for was 5.4.5.1 (Red Hat), PerlMagick was 5.43
Any suggestions are most welcome - please let me know if more information would be helpful.
We were using an older version of ImageMagick / PerlMagick on a Red Hat 7.3 server until last week when it went kaputski. I set up an Edgy (Ubuntu 6.10) server with LAMP, added mod_perl and the latest imageMagick / PerlMagick updates (6.3.3-4). It may be worth noting that I'm a beginner when it comes to Linux ops, so bear with me...
The site is primarily HTML and PERL scripts that puts together timecard forms with user data and images (where ImageMagick comes in). The idea is for the user to upload whatever image they want and the site resizes and dumps it in a shared folder that the images are then drawn from later when a user views employee information.
The site's functionality works fine, save for the upload script that allows a user to find an image on their desktop and upload it to the server. The script below parses fine, but the image is not actually uploaded. If I manually copy an image into the server's folder with the proper name (employee number + .jpg), it displays fine, however. If you try to upload something when an image for that employee is already present, the old photo does not change. Nor does it place a new photo if one is not already present.
The code did work under the previous server.
The upload script is as follows:
***************************************
#!/usr/bin/perl
use DB_File;
use Image::Magick;
sub endit
{
$_[0] =~ s/^.*\=\"//;
$_[0] =~ s/\".*$//;
}
print "Content-type: text/html\n\n";
open (FILE, "-");
$boundary = <FILE>;
chomp ($boundary);
$blength = length ($boundary);
$junk = <FILE>;
$junk = <FILE>;
$sess = <FILE>;
chomp $sess;
$sess =~ s/\s//g;
$junk = <FILE>;
$junk = <FILE>;
$junk = <FILE>;
$empnum = <FILE>;
chomp $empnum;
$empnum =~ s/\s//g;
$junk = <FILE>;
$junk = <FILE>;
$junk = <FILE>;
$rotate = <FILE>;
chomp $rotate;
$rotate =~ s/\s//g;
$junk = <FILE>;
$filename = <FILE>;
endit ($filename);
$type = <FILE>;
chomp $type;
$junk = <FILE>;
$ext = "";
if ($filename =~ /\.jpg/i) { $ext = ".jpg"; }
if ($filename =~ /\.jpeg/i) { $ext = ".jpg"; }
if ($filename =~ /\.gif/i) { $ext = ".gif"; }
if ($filename =~ /\.png/i) { $ext = ".png"; }
if ($type =~ /jpg/i) { $ext = ".jpg"; }
if ($type =~ /jpeg/i) { $ext = ".jpg"; }
if ($type =~ /gif/i) { $ext = ".gif"; }
if ($type =~ /png/i) { $ext = ".png"; }
$temp = sprintf("upload%5d", $$);
$temp =~ s/\s/0/g;
$name = $temp . $ext;
$jname = $empnum . ".jpg";
$thumb = $empnum . "t.jpg";
unlink "photos/$name";
$size = 0;
if ($ext ne "")
{
open (IMAGE, ">photos/$name");
while (<FILE>)
{
if (substr ($_, 0, $blength) eq $boundary) { last; }
print IMAGE "$_";
$size = $size + length ($_);
}
close (IMAGE);
$t = chmod 0660, "photos/$name";
}
close (FILE);
$image = Image::Magick->new;
$image->Read("photos/$name");
unlink "photos/$name";
if ($rotate =~ /l/i)
{
$image->Set(bordercolor=>'#ffffff');
$image->Rotate(degrees=>270, crop=>0, sharpen=>0);
}
if ($rotate =~ /r/i)
{
$image->Set(bordercolor=>'#ffffff');
$image->Rotate(degrees=>90, crop=>0, sharpen=>0);
}
$image->Crop('0x0');
($h, $w, $d) = $image->Get('rows', 'columns', 'depth');
if ($w > ($h * 0.75))
{
$tw = ($w - $h * 0.75) / 2;
$w = $h * 0.75;
$image->Crop(width=>$w, height=>$h, x=>$tw, y=>0);
}
if ($h > ($w * 1.333))
{
$th = ($h - $w * 1.333) / 2;
$h = $w * 1.333;
$image->Crop(width=>$w, height=>$h, x=>0, y=>$th);
}
if ($h > 640)
{
$h = 640;
$w = 480;
$scale = "$w" . "x" . "$h";
$image->Scale($scale);
}
$image->Write("jpeg:photos/$jname");
if ($h > 160)
{
$h = 160;
$w = 120;
$scale = "$w" . "x" . "$h";
$image->Scale($scale);
$image->Sharpen('50');
}
$image->Set(quality=>50);
$image->Write("jpeg:photos/$thumb");
undef $image;
$t = chmod 0660, "$imageloc/upload/$jname";
$t = chmod 0660, "$imageloc/upload/$thumb";
print "
<html>
<head>
<title>Human resources - display employee info</title>
</head>
<body bgcolor=white background=/images/aslogob1.jpg>
<table width=100% cols=1><tr><td bgcolor=$color>
<font size=+4 color=white>
<i> Audiosears Corporation</i>
</font>
<hr>
<img src=fetchimg?images/aslogwt.gif align=right>
<font size=+2 color=white>
<b> Human Resources</b><br>
<b> Display Employee Information</b>
</font>
<br></table><br>
";
if ($empnum > 0)
{
tie %employee, 'DB_File', "employee.db";
@fields = split (/\|/, $employee{$empnum});
untie %employee;
print "
<img src=fetchimg?photos/$empnum.jpg align=left height=320 width=240>
<table cols=2 width=350 border=0>
<tr valign=top>
<td align=right><font size=+1>Number:
<td><font size=+1>$empnum
<tr valign=top>
<td align=right><font size=+1>Name:
<td><font size=+1>$fields[1] $fields[2] $fields[0]
<tr valign=top>
<td align=right><font size=+1>Status:
<td><font size=+1>$fields[3]
<tr valign=top>
<td align=right><font size=+1>Department:
<td><font size=+1>$fields[4]
<tr valign=top>
<td align=right><font size=+1>Title:
<td><font size=+1>$fields[5]
<tr valign=top>
<td align=right><font size=+1>Class:
<td><font size=+1>$fields[6]
<tr valign=top>
<td align=right><font size=+1>Location:
<td><font size=+1>$fields[7]
</table>
<br clear=all><br><hr><br>
";
}
print "
<table cols=2 width=2 border=0><tr valign=top>
<td width=1>
<form method=post action=timecard>
<input type=hidden name=sess value=\"$sess\">
<input type=hidden name=num1 value=$empnum>
<input type=submit value=\"Print Timecard\">
</form>
<td width=1>
<form method=post action=editinfo>
<input type=hidden name=sess value=\"$sess\">
<input type=hidden name=empnum value=$empnum>
<input type=submit value=\"Edit Employee\">
</form>
</table>
<form method=post action=empinfo>
<input type=hidden name=sess value=\"$sess\">
<input type=text name=empnum value=\"\" size=6 maxlength=6>
<input type=submit value=\"View Employee\">
</form>
<form method=post action=emplookup>
<input type=hidden name=sess value=\"$sess\">
<input type=text name=search value=\"\" size=20 maxlength=20>
<input type=submit value=\"Search\">
</form>
</body>
</html>
";
**************************************************
The 'photos' folder is located at /server/humanres/photos - the script above and all other server scripts are in the humanres folder and executable by Apache. So far, I can only think of the following as issues:
1) Permissions issue in photos folder for posting uploads? Right now permissions on it are wide open (for testing on localhost) - should that folder be owned by a specific user or group?.
2) Version conflicts of ImageMagick - this code was written for was 5.4.5.1 (Red Hat), PerlMagick was 5.43
Any suggestions are most welcome - please let me know if more information would be helpful.