Database Reference
In-Depth Information
terminal window to turn into a horrible mess of gibberish, or possibly even lock up. It's
more common to use the information for display in a web page. Or you might send it
to the client for downloading (see Recipe 19.9 ), although that is more common for
nonimage binary data such as PDF files.
To display an image in a web page, include an <img> tag in the page that tells the client's
web browser where to get the image. If you've stored images as files in a directory to
which the web server has access, you can refer to an image directly. For example, if the
image file iceland.jpg is located in the /usr/local/lib/mcb/images directory, refer to it like
this:
<img src= "/usr/local/lib/mcb/images/iceland.jpg" />
With this approach, make sure that each image filename has an extension (such as .gif
or .png ) that enables the web server to determine what kind of Content-Type: header
to generate when it sends the file to the client.
If the images are stored in a database table instead, or in a directory inaccessible to the
web server, the <img> tag can refer to a script that knows how to fetch images and send
them to clients. To do this, the script must respond by sending a Content-Type: header
that indicates the image format, a Content-Length: header that indicates the number
of bytes of image data, a blank line, and finally the image itself as the body of the response.
The following script, display_image.pl , demonstrates how to serve images over the Web.
It requires a name parameter that indicates which image to display, and permits an op‐
tional location parameter that specifies whether to retrieve the image from the im
age table or from the filesystem. The default is to retrieve image data from the image
table. For example, the following URLs display an image from the database and from
the filesystem, respectively:
http://localhost/cgi-bin/display_image.pl?name=iceland.jpg
http://localhost/cgi-bin/display_image.pl?name=iceland.jpg;location=fs
The script looks like this:
#!/usr/bin/perl
# display_image.pl: display image over the Web
use strict ;
use warnings ;
use CGI qw(:standard escapeHTML) ;
use FileHandle ;
use Cookbook ;
sub error
{
my $msg = escapeHTML ( $_ [ 0 ]);
print header (), start_html ( "Error" ), p ( $msg ), end_html ();
exit ( 0 );
Search WWH ::




Custom Search