Problem Uploading files via Perlmagick / LAMP Server
Posted: 2007-04-02T15:10:50-07:00
Hey there everyone...
I'm new to PerlMagick and am trying to get some old code back up and running from a previous programmer who has long since left the company, originally developing via ImageMagick 5.4.5.1, PerlMagick 5.43.
The following was designed to work with other modules to upload pictures into a folder on a Red Hat Linux server for an employee database. The filename corresponds with the employee number (hence the code to parse the filename), and a jpg is created from whatever the user selects to upload.
This used to work fine on the old server with the above ImageMagick/PerlMagick distro, but our installs for those packages are no longer available, so I'm trying this with ImageMagick 6.3.3-4 on a new Ubuntu 6.10 server.
The code parses fine with perl -w <code>, but it doesn't actually upload to the drive, and the picture remains unchanged. I currently have the permissions set wide open for testing, but the key may lie in a syntax or usage issue between ImageMagick versions...
***********************************
#!/usr/bin/perl
use DB_File;
use Image::Magick;
sub endit
{
$_[0] =~ s/^.*\=\"//;
$_[0] =~ s/\".*$//;
}
print "Content-type: text/html\n\n";
# Parses the DB file and finds the appropriate filename
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>;
# Handlers for different graphic types
$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;
($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";
*********************************************
Looking at some code examples, I think the issue is in the $image->Write("jpeg:photos/$thumb"); or the $t = chmod 0660 <etc etc> statements, but I am vigorously thankful for any input, especially if there are other ways this might be improved...
I'm new to PerlMagick and am trying to get some old code back up and running from a previous programmer who has long since left the company, originally developing via ImageMagick 5.4.5.1, PerlMagick 5.43.
The following was designed to work with other modules to upload pictures into a folder on a Red Hat Linux server for an employee database. The filename corresponds with the employee number (hence the code to parse the filename), and a jpg is created from whatever the user selects to upload.
This used to work fine on the old server with the above ImageMagick/PerlMagick distro, but our installs for those packages are no longer available, so I'm trying this with ImageMagick 6.3.3-4 on a new Ubuntu 6.10 server.
The code parses fine with perl -w <code>, but it doesn't actually upload to the drive, and the picture remains unchanged. I currently have the permissions set wide open for testing, but the key may lie in a syntax or usage issue between ImageMagick versions...
***********************************
#!/usr/bin/perl
use DB_File;
use Image::Magick;
sub endit
{
$_[0] =~ s/^.*\=\"//;
$_[0] =~ s/\".*$//;
}
print "Content-type: text/html\n\n";
# Parses the DB file and finds the appropriate filename
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>;
# Handlers for different graphic types
$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;
($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";
*********************************************
Looking at some code examples, I think the issue is in the $image->Write("jpeg:photos/$thumb"); or the $t = chmod 0660 <etc etc> statements, but I am vigorously thankful for any input, especially if there are other ways this might be improved...