Subversion Repositories CoffeeCatalog

Rev

Rev 8 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8 Rev 9
Line 2... Line 2...
2
 
2
 
3
use strict;
3
use strict;
4
use CGI qw(:standard escape escapeHTML);
4
use CGI qw(:standard escape escapeHTML);
Line 5... Line 5...
5
use WebDB;
5
use WebDB;
6
 
6
 
-
 
7
if (defined (param ("roaster"))) {
7
if (defined (param ("name"))) {
8
  my $roaster = param ("roaster");
-
 
9
  my $coffee = param ("coffee");
8
  my $image = param ("name");
10
  my $thumb = param ("thumbnail");
-
 
11
  
9
  my $thumb = param ("thumbnail");
12
  display_image ($roaster, $coffee, $thumb);
10
	display_image ($image, $thumb);
13
  
11
} elsif (defined (param ("gallery"))) {
14
} elsif (defined (param ("gallery"))) {
12
	display_gallery ()
15
	display_gallery ()
13
} else {
16
} else {
Line 14... Line 17...
14
	error ("Unknown request type");
17
	error ("Unknown request type");
15
}
18
}
-
 
19
 
16
 
20
 
17
 
21
sub display_image {
18
sub display_image {
-
 
19
	my ($name, $show_thumbnail) = @_;
22
	my ($R, $C, $show_thumbnail) = @_;
-
 
23
 
20
	my $col_name = (defined ($show_thumbnail) ? "thumbnail" : "logo");
24
	my $col_name = (defined ($show_thumbnail) ? "thumbnail" : "logo");
-
 
25
	my ($dbh, $mime_type, $data);
-
 
26
	$dbh = WebDB::connect ();
-
 
27
	if (defined ($C)) {
-
 
28
	  ($mime_type, $data) = $dbh->selectrow_array (
-
 
29
                     "SELECT mime_type, $col_name FROM coffees WHERE roaster = ? and coffee = ?",
-
 
30
                      undef, $R, $C);
-
 
31
#  If there's no coffee image found, display the roaster image instead.
-
 
32
    unless (defined $mime_type) {
-
 
33
  	  ($mime_type, $data) = $dbh->selectrow_array (
-
 
34
                     "SELECT mime_type, $col_name FROM roasters WHERE roaster = ?",
21
	my ($dbh, $mime_type, $data);
35
                      undef, $R);
22
	
36
    }
-
 
37
	} else {
-
 
38
	  ($mime_type, $data) = $dbh->selectrow_array (
23
	$dbh = WebDB::connect ();
39
                     "SELECT mime_type, $col_name FROM roasters WHERE roaster = ?",
Line -... Line 40...
-
 
40
                      undef, $R);
-
 
41
	}
-
 
42
	
-
 
43
	$dbh->disconnect ();
-
 
44
	
-
 
45
#-----------------------------------------------------------------
-
 
46
  if ($col_name eq "logo") {
-
 
47
  	use Image::Magick;
-
 
48
  	my $img = new Image::Magick;
-
 
49
  	my $err = $img->BlobToImage ($data);
-
 
50
  	error ("Can't convert image data: $err") if $err;
-
 
51
  	
-
 
52
  	if ($img->Get("width") > 300 or $img->Get("height") > 300) {
-
 
53
  	  $err = $img->Scale (geometry => "300x300");
-
 
54
  	  error ("Can't scale image file: $err") if $err;
24
	($mime_type, $data) = $dbh->selectrow_array (
55
  	}
25
                     "SELECT mime_type, $col_name FROM roasters WHERE roaster = ?",
56
    $data = $img->ImageToBlob ();
Line 26... Line 57...
26
                      undef, $name);
57
  }
27
	$dbh->disconnect ();
58
#-----------------------------------------------------------------
Line 28... Line 59...
28
	
59
 
Line 44... Line 75...
44
	# we're fetching a single value (name), so we can call fetchrow_array()
75
	# we're fetching a single value (name), so we can call fetchrow_array()
45
	# in a scalar context to get the value
76
	# in a scalar context to get the value
Line 46... Line 77...
46
	
77
	
47
	while (my $name = $sth->fetchrow_array ()) {
78
	while (my $name = $sth->fetchrow_array ()) {
48
		# encode the name with escape() for the URL, with escapeHTML() otherwise
79
		# encode the name with escape() for the URL, with escapeHTML() otherwise
49
		my $url = url () . sprintf ("?name=%s", escape ($name));
80
		my $url = url () . sprintf ("?roaster=%s", escape ($name));
50
		$name = escapeHTML ($name);
81
		$name = escapeHTML ($name);
51
		print p ($name),
82
		print p ($name),
52
					a ({-href => $url},     # link for full size image
83
					a ({-href => $url},     # link for full size image
53
                  # embed thumbnail as the link content to make it clickable
84
                  # embed thumbnail as the link content to make it clickable