Subversion Repositories CoffeeCatalog

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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