#!/usr/bin/perl #********************************************************************** #* Program: Image Randomizer from Radiation * #* Version: 1.0 * #* Author: Andrew Cowan (icculus@radiation.com) for Radiation * #* Date: January 7, 1997 * #* Product Info: http://www.radiation.com/gizmos/random.html * #* * #* This program is the copyrighted property of Radiation, a venture * #* of GlobalMedia Design. Any use of this program is subject to the * #* the terms of the license agreement (LICENSE.TXT) included as part * #* of this distribution archive. Any other uses are stictly * #* prohibited without the written permission of GlobalMedia Design * #* and all other rights are reserved. * #* * #* Support for this software is available only to registered users. * #* For registration information, consult the REGISTER.TXT document * #* included as part of this distribution archive, or visit the Gizmos * #* section of the Radiation website at: * #* * #* http://www.radiation.com/gizmos/ * #* * #********************************************************************** # ************************************************************************ # USAGE: # # Just drop the script into a CGI accessible directory, make it executable # and make sure the perl location at the top is correct. # # Information is passed to the script via form fields passed in the url. # # Fields: # dir - The absolute directory location of the images directory # type - Either GIF or JPG, depending on which image type you want # # So, an example call to the script: # # http://www.host.com/cgi-bin/random_image.cgi?dir=/www/htdocs/images&type=GIF # http://www.metrarail.com/cgi/random_image.cgi?dir=Rotate/images&type=JPG # ************************************************************************ # Main Routine # ************************************************************************ &cgiparse; &startup; &grab_image; &print_image; exit(0); # ************************************************************************ # Subroutine Declarations # ************************************************************************ sub startup{ # Get the given form fields $dir = $elements{'dir'}; $type = $elements{'type'}; } sub grab_image{ # Grab all files in the given directory opendir(DIR, "$dir"); local(@temp_files) = readdir(DIR); closedir(DIR); # Grab only appropriate files based on type of image file foreach $file (@temp_files){ if ($type eq "GIF"){ if ($file =~ /\.gif$/){ push(@file_list, $file); } }elsif (($type eq "JPEG") || ($type eq "JPG")){ if ($file =~ /\.jpe?g$/){ push(@file_list, $file); } }else{ if ($file =~ /\.jpe?g$/){ push(@file_list, $file); } } } } sub print_image{ # Determine number of images local($num_images) = $#file_list + 1; # Grab a random number based on number of image files srand; local($index) = int(rand($num_images)); # Construct full filename using given directory local($filename) = $dir . "/" . $file_list[$index]; # Determine and print the proper content type if ($filename =~ /\.gif$/){ print "Content-type: image/gif \n\n"; }elsif ($filename =~ /\.jpe?g$/){ print "Content-type: image/jpeg \n\n"; } $| = 1; # Force flush after a write # Read the file and print line by line, flushing as we go open(IMAGE, "$filename"); while(){ print; } close(IMAGE); } # ************************************************************************ # Urldecode Routines # ************************************************************************ # Copyright & Disclaimer. (James Tappin) # The following set of routines may be freely distributed, modified # and used, provided this copyright & disclaimer remains intact. # This package is used at your own risk, if it does what you # want, good; if it doesn't, modify it or use something else--but # don't blame me. Support level = negligable (i.e. mail bugs but # not requests for extensions) # ************************************************************************ sub cgiparse { if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $data, $ENV{'CONTENT_LENGTH'}); } elsif ($ENV{'REQUEST_METHOD'} eq "GET") { $data = $ENV{'QUERY_STRING'}; } else { # # Bad request method report and exit. # &method_error; } %elements = &url_decode(split(/[&=]/,$data)); %elements; } sub method_error { print "\nCGI Script Requires use of method POST or GET.\n"; exit; } sub url_decode { foreach (@_) { tr/+/ /; s/%(..)/pack("c",hex($1))/ge; } @_; } 1;