HPS IT Banner
Home Date-book NMS Library


NMS Home Page Image Generating Script

The CGI script below is called through a standard HTML image tag:

<img src=/cgi-bin/scripts/imagetool/homepage.pl alt=" Random spring image ">

Normally the tag would have an image file name following the img=, here the script returns a random image from a pool of predefined images.



#!/usr/contrib/bin/perl
#
# homepage.pl
#
# Script to hand out random image from a pool for the NMS home page
#
# Note: return_graphic emits a pragma to tell the browser not to cache the
# image.  It's not that we care about caching, so much as we want Netscape
# to behave better!
#

$image_file_path = "/home/apache/cgi-bin/scripts/imagetool/images";
$version = "1.0";

# -------------------- Nothing but code below this point ---------------------

@image_list = ("img1.jpg","img2.jpg","img3.jpg","img4.jpg","img5.jpg");
$num_images = @image_list;

srand(time() ^ ($$ + ($$ <<  15))); # Set a *random* seed number for rand()

$rawnum = rand $num_images;       # Get a randum no. (0 - number of images)

$file = "$image_list[$rawnum]";   # Set the file from list based on rand. no.

$fullfname = "$image_file_path/$file";

return_graphic($fullfname);       # Return graphic file


#####################################################################
####   Subroutines from here on down . . .                       ####
#####################################################################



sub return_graphic {              # Returns the chosen image file to the 
                                  # browser w/proper headers

  my $file;
  $file = shift;                  # Grab the file name from our arg list

                                  # If we can't open the image file, bail out
  open IMAGE, $file or not_found();

  print "Pragma: no-cache\n";
  print "Content-type: image/jpeg\n\n";

  binmode STDOUT;
  my $buffer = "";
  while ( read( IMAGE, $buffer, 16_384 ) ) {
    print $buffer;
  }
  close IMAGE;

} # End return_graphic();



sub not_found {                   # 404 Not Found doc to be returned on an
                                  # image file open error
  print << END_OF_ERROR;
Status: 404 Not Found
Content-type text/html

<html> 
<head> 
  <title> File Not Found</title> 
</head> 
<body> 
  <h1> File $file Not Found</h1> 

</body> 
</html> 
END_OF_ERROR

  exit;

} # End not_found();

# End.








Hartford Public Schools Information Technology Department | Updated 4-17-02 Back to top