Rev 8 | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/perl -wuse strict;use CGI qw(:standard escape escapeHTML);use WebDB;if (defined (param ("roaster"))) {my $roaster = param ("roaster");my $coffee = param ("coffee");my $thumb = param ("thumbnail");display_image ($roaster, $coffee, $thumb);} elsif (defined (param ("gallery"))) {display_gallery ()} else {error ("Unknown request type");}sub display_image {my ($R, $C, $show_thumbnail) = @_;my $col_name = (defined ($show_thumbnail) ? "thumbnail" : "logo");my ($dbh, $mime_type, $data);$dbh = WebDB::connect ();if (defined ($C)) {($mime_type, $data) = $dbh->selectrow_array ("SELECT mime_type, $col_name FROM coffees WHERE roaster = ? and coffee = ?",undef, $R, $C);# If there's no coffee image found, display the roaster image instead.unless (defined $mime_type) {($mime_type, $data) = $dbh->selectrow_array ("SELECT mime_type, $col_name FROM roasters WHERE roaster = ?",undef, $R);}} else {($mime_type, $data) = $dbh->selectrow_array ("SELECT mime_type, $col_name FROM roasters WHERE roaster = ?",undef, $R);}$dbh->disconnect ();#-----------------------------------------------------------------if ($col_name eq "logo") {use Image::Magick;my $img = new Image::Magick;my $err = $img->BlobToImage ($data);error ("Can't convert image data: $err") if $err;if ($img->Get("width") > 300 or $img->Get("height") > 300) {$err = $img->Scale (geometry => "300x300");error ("Can't scale image file: $err") if $err;}$data = $img->ImageToBlob ();}#-----------------------------------------------------------------# did we find a record?error ("Cannot find image for $R") unless defined ($mime_type);print header (-type => $mime_type, -Content_Length => length ($data)), $data;}sub display_gallery {my ($dbh, $sth);print header (), start_html ("Image Gallery");$dbh = WebDB::connect ();$sth = $dbh->prepare ("SELECT roaster FROM roasters ORDER BY roaster");$sth->execute ();# we're fetching a single value (name), so we can call fetchrow_array()# in a scalar context to get the valuewhile (my $name = $sth->fetchrow_array ()) {# encode the name with escape() for the URL, with escapeHTML() otherwisemy $url = url () . sprintf ("?roaster=%s", escape ($name));$name = escapeHTML ($name);print p ($name),a ({-href => $url}, # link for full size image# embed thumbnail as the link content to make it clickableimg ({-src => "$url;thumbnail=1", -alt => $name})),"\n";}$sth->finish ();$dbh->disconnect ();print end_html ();}sub error {my $msg = shift;print header (),start_html ("Error"),p (escapeHTML ($msg)),end_html ();exit (0);}