freelanceprogrammers.org Forum Index » Perl

Image uploading and storing issues


View user's profile Post To page top
kjhseka Posted: Mon Jun 27, 2005 12:06 am


Joined: 14 Jun 2004

Posts: 4
Image uploading and storing issues
Hello,
I have a script with several problems on it. The script is taken from
MySql and Perl

from the Web by Paul Dubois.

First of all, I run it into Windows XP system, with Apache server and
MySql. I use IE

6 to test it. The script should upload an image and store it in the
database. I try

the script locally.

But...

1) If I try to upload an image, actually no data is stored in the
database! The

database is defined as follow:

CREATE TABLE image (name VARCHAR(60) NOT NULL, UNIQUE(name), image
BLOB NOT NULL,

thumbnail BLOB NOT NULL, mime_type VARCHAR(20) NOT NULL);

2) $image variable in my $image = param("image"); should store the
binary data of the

image, but it actually only store the path and filename! Is that
normal? Why the

variable does not store the binary data?

Another thing... The Image::Magick module does no longer exist! Is
never installed in

most web servers and hosting services and is very difficult to get
it. What kind of

module is used right now to process images?

Here`s the script, the main problem is that no data is stored in the
database, there

is surely some error, but I`m unable to locate it. Or perhaps in
Windows image

uploading works differently?


#! d:/perl/bin/perl.exe
# upload_image.pl - upload images for e-greeting card application

use strict;
use CGI qw(:standard escape escapeHTML);

print header(),
start_html (-title => "Image Upload", -bgcolor => "white");

# Dispatch to proper action based on user selection

#@ DISPATCH
my $choice = lc(param("choice"));

if($choice eq "")
{
display_upload_form();
}
elsif ($choice eq "submit")
{
process_form();
}
else
{
print p (escapeHTML ("Logic error, unknown choice :
$choice"));
}
#@ DISPATCH

print end_html();
exit (0);

# Display the image uploading form. Must use multipart encoding for a
form that
# contain a file upload field.

#@ DISPLAY_UPLOAD_FORM
sub display_upload_form
{
print start_multipart_form(-action => url()),
"Image file: ", br(),
filefield(-name => "image", -size => 60),
br(),
"Descriptive name for image: ", br(),
textfield( -name => "name", -value => "", -override
=> 1, -size =>

60),
br(), br(),
submit(-name=>"choice", -value => "Submit"),
end_form();
}

#@ DISPLAY_UPLOAD_FORME

#@ PROCESS_FORM
sub process_form
{
my $name = param("name");
my $image = param("image");
my @errors = ();
my $dbh;
my $mime_type;
my ($full, $thumb);
my $serve_url;

print $image;

$image = "" unless defined($image); # convert undef to empty
string
$name =~ s/^s+//;
$name =~ s/s+$//;

# check for required fields
push(@errors, "Please supply an image name") if $name eq "";
push(@errors, "Please specify an image file") if $image eq "";

if(@errors)
{
print p("The following error(s) occurred:");
print ul(li(@errors));
print p("Please, click your browser`s back button
to
"
. "return to the previous page and correct
the problem.");
return;
}

# Form was okay; get image type and contents and create new
record.
# Use REPLACE rather than INSERT to make it easy to ovrwrite
an existing image
# with the same name with a new one. ISERT would generate an
error and INSERT
# IGNORE would keep the old image. So we need REPLACE.

$mime_type = uploadInfo($image)->{`Content-Type`};
($full, $thumb) = read_image_file($image);

$dbh = DBI->connect
("DBI:mysql:host=localhost;database=webdb", "webdev",

"webdevpass"), {PrintError=>0, RaiseError=>1};

$dbh->do("REPLACE INTO image (name, image, thumbnail,
mime_type),

VALUES(?,?,?,?)", undef, $name, $image, $thumb, $mime_type);

$dbh->disconnect();

# Image was stored into database successfully. Present
confirmation
# page that displays both the full size and thumbnail images.

print p ("The image upload was successful.");

# Encode the name with escape() for URL, but with escapeHTML
() otherwise
$serve_url = sprintf("serve_image.pl?name=%s", escape($name));
$name = escapeHTML($name);
$mime_type = escapeHTML($mime_type);

print p("Image name: $name"),
p("MIME type: $mime_type"),
p("Full size image:"),
img({-src=>$serve_url, -alt=>$name}), "
",
p("Thumbnail image:"),
img({-src=>"$serve_url;thumbnail=1", -alt =>
$name}), "
";

# Display link to main page so user can upload
another image
print hr(), a({-href=>url()}, "Upload next image");
}
#@ PROCESS_FROM

# Read contents of an uploaded file. $fh is the filehandle (it
actually can be
# treated as either a string or a filehandle, per CGI.pm convention).
# The function reads the file directly rather than passing the
filehandle
# to $img->Read() doesn`t work properly then for image uploads from
Windows machines.

#@ READ_IMAGE_FILE
sub read_image_file
{
my $fh = shift; # filename/file handle
my $img = new Image::Magick;
my ($full, $thumb);
my $err;

# read full-size image directly from upload file
(read ($fh, $full, -s $fh) == -s $fh)
or error ("Can`t read image file: $!");
# produce thumbnail from full-size image
$err = $img->BlobToImage ($full);
error ("Can`t convert image data: $err") if $err;
$err = $img->Scale (geometry => "64x64");
error ("Can`t scale image file: $err") if $err;
$thumb = $img->ImageToBlob ();
return ($full, $thumb);
}
#@ READ_IMAGE_FILE

#@ ERROR
sub error
{
my $msg = shift;
print p (escapeHTML ("Error: $msg")), end_html();
exit(0);
}
#@ ERROR

Thanks for any help.

Alph
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help