| 2 |
- |
1 |
#!/usr/bin/perl -w
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use CGI qw(:standard escapeHTML);
|
|
|
5 |
print header (),
|
|
|
6 |
start_html (-title => "Form Element Demonstration", -bgcolor => "white");
|
|
|
7 |
|
|
|
8 |
if (defined (param ("name"))) {
|
|
|
9 |
process_form ();
|
|
|
10 |
} else {
|
|
|
11 |
display_upload_form ();
|
|
|
12 |
}
|
|
|
13 |
|
|
|
14 |
print end_html ();
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
sub display_upload_form {
|
|
|
18 |
print start_multipart_form (-action => url ()),
|
|
|
19 |
"Image file: ", br (),
|
|
|
20 |
filefield (-name => "image", -size => 60),
|
|
|
21 |
br (),
|
|
|
22 |
"Descriptive name for image: ", br (),
|
|
|
23 |
textfield (-name => "name",
|
|
|
24 |
-value => "",
|
|
|
25 |
-override => 1,
|
|
|
26 |
-size => 60),
|
|
|
27 |
br (), br (),
|
|
|
28 |
submit (-name => "choice", -value => "Submit"),
|
|
|
29 |
end_form ();
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
sub process_form {
|
|
|
33 |
my $name = param ("name"); # image name
|
|
|
34 |
my $image = param ("image"); # image file
|
|
|
35 |
my @errors = ();
|
|
|
36 |
my $dbh;
|
|
|
37 |
my $mime_type;
|
|
|
38 |
my ($full, $thumb);
|
|
|
39 |
my $serve_url;
|
|
|
40 |
$image = "" unless defined ($image); # convert undef to empty string
|
|
|
41 |
$name = WebDB::trim ($name); # trim extraneous whitespace from name
|
|
|
42 |
# check for required fields
|
|
|
43 |
push (@errors, "Please supply an image name") if $name eq "";
|
|
|
44 |
push (@errors, "Please specify an image file") if $image eq "";
|
|
|
45 |
if (@errors) {
|
|
|
46 |
print p ("The following errors occurred:");
|
|
|
47 |
print ul (li (\@errors));
|
|
|
48 |
print p ("Please click your Browser's Back button to\n"
|
|
|
49 |
. "return to the previous page and correct the problem.");
|
|
|
50 |
return;
|
|
|
51 |
} # Form was okay; get image type and contents and create new record.
|
|
|
52 |
# Use REPLACE to clobber any old image with the same name.
|
|
|
53 |
$mime_type = uploadInfo ($image)->{'Content-Type'};
|
|
|
54 |
($full, $thumb) = read_image_file ($image);
|
|
|
55 |
$dbh = WebDB::connect ();
|
|
|
56 |
$dbh->do (
|
|
|
57 |
"REPLACE INTO image
|
|
|
58 |
(name,image,thumbnail,mime_type)
|
|
|
59 |
VALUES(?,?,?,?)",
|
|
|
60 |
undef,
|
|
|
61 |
$name, $full, $thumb, $mime_type);
|
|
|
62 |
$dbh->disconnect (); # Image was stored into database successfully. Present confirmation
|
|
|
63 |
# page that displays both the full size and thumbnail images.
|
|
|
64 |
print p ("The image upload was successful.");
|
|
|
65 |
# encode the name with escape() for URL, but with escapeHTML() otherwise
|
|
|
66 |
$serve_url = sprintf ("serve_image.pl?name=%s", escape ($name));
|
|
|
67 |
$name = escapeHTML ($name);
|
|
|
68 |
$mime_type = escapeHTML ($mime_type);
|
|
|
69 |
print p ("Image name: $name"),
|
|
|
70 |
p ("MIME type: $mime_type"),
|
|
|
71 |
p ("Full size image:"),
|
|
|
72 |
img ({-src => $serve_url, -alt => $name}), "\n",
|
|
|
73 |
p ("Thumbnail image:"),
|
|
|
74 |
img ({-src => "$serve_url;thumbnail=1", -alt => $name}), "\n";
|
|
|
75 |
# Display link to main page so user can upload another image
|
|
|
76 |
print hr (), a ({-href => url ()}, "Upload next image");
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
use Image::Magick;
|
|
|
80 |
sub read_image_file {
|
|
|
81 |
my $fh = shift; # filename/file handle
|
|
|
82 |
my $img = new Image::Magick;
|
|
|
83 |
my ($full, $thumb);
|
|
|
84 |
my $err;
|
|
|
85 |
# read full-size image directly from upload file
|
|
|
86 |
(read ($fh, $full, (stat ($fh))[7]) == (stat ($fh))[7])
|
|
|
87 |
or error ("Can't read image file: $!");
|
|
|
88 |
# produce thumbnail from full-size image
|
|
|
89 |
$err = $img->BlobToImage ($full);
|
|
|
90 |
error ("Can't convert image data: $err") if $err;
|
|
|
91 |
$err = $img->Scale (geometry => "64x64");
|
|
|
92 |
error ("Can't scale image file: $err") if $err;
|
|
|
93 |
$thumb = $img->ImageToBlob ();
|
|
|
94 |
return ($full, $thumb);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
sub error {
|
|
|
98 |
my $msg = shift;
|
|
|
99 |
print p (escapeHTML ("Error: $msg")), end_html ();
|
|
|
100 |
exit (0);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|