#!/usr/bin/perl -Tw # serve_image.cgi - serve an image file as a Web page use CGI qw(:standard escape escapeHTML); use DBI; use strict; use CGI::Carp qw(fatalsToBrowser); use lib qw(/usr50/home/summitwe/public_html/library); use WebDB; #@ DISPATCH if (defined (param ("item_id"))) { display_image (param ("item_id"), param ("thumbnail")); } elsif (defined (param ("gallery"))) { display_gallery () } else { error ("Unknown request type"); } #@ DISPATCH exit (0); #@ DISPLAY_IMAGE sub display_image { my ($item_id, $show_thumbnail) = @_; my $col_name = (defined ($show_thumbnail) ? "thumbnail" : "picture"); my ($dbh, $mime_type, $data); $dbh = WebDB::connect (); ($mime_type, $data) = $dbh->selectrow_array ( "SELECT mime_type, $col_name FROM catalog_pet WHERE item_id = ?", undef, $item_id); $dbh->disconnect (); # did we find a record? error ("Cannot find image named $item_id") unless defined ($mime_type); print header (-type => $mime_type, -Content_Length => length ($data)), $data; } #@ DISPLAY_IMAGE # Present gallery of names and images in the image table. Present the # thumbnail version of each image, but embed the image inside a hyperlink # that selects the full size image for display. #@ DISPLAY_GALLERY sub display_gallery { my ($dbh, $sth); print header (), start_html ("Image Gallery"); $dbh = WebDB::connect (); $sth = $dbh->prepare ("SELECT item_id FROM catalog_pet ORDER BY item_id"); $sth->execute (); # we're fetching a single value (name), so we can call fetchrow_array() # in a scalar context to get the value while (my $item_id = $sth->fetchrow_array ()) { # encode the name with escape() for the URL, with escapeHTML() otherwise my $url = url () . sprintf ("?item_id=%s", escape ($item_id)); $item_id = escapeHTML ($item_id); print p ($item_id), a ({-href => $url}, # link for full size image # embed thumbnail as the link content to make it clickable img ({-src => "$url;thumbnail", -alt => $item_id}) ), "\n"; } $sth->finish (); $dbh->disconnect (); print end_html (); } #@ DISPLAY_GALLERY #@ ERROR sub error { my $msg = shift; print header (), start_html ("Error"), p (escapeHTML ($msg)), end_html (); exit (0); } #@ ERROR