random.pl script edited

Kinda funny. After all these years using this perl CGI script just using it as best as I could and not knowing anything about scripting, I finally tried modifying it so it will also work with .png images. And it worked.

My edits (added code) is in red….


#!/usr/bin/perl
#
#FILE: random.pl - Version 1.1
#
# This program now works with both Unix and WindowsNT
#
# This is a very simple perl script. All it does is grab a
#random image from a directory and print it out. Currently, this
#script just works with gif and jpg/jpeg images. To call this
#script from your HTML page:
# 1) Copy the perl script to your cgi-bin directory, or wherever
#cgi scripts can be executed on your system.
# 2) Make sure the script has execute permissions.
# 3) Make sure your $IMAGE_DIRECTORY has read permissions, as well
# as the files within that directory.
# 4) Make your IMG tag look similar to the following:
#
#
#The full path listing to the directory which contains the images
#to be randomly chosen and displayed.
#Unix users should be able to get away with specifying a relative path.
#NT users may have to specify the full system path, which will probably
#need to look similar to: "c:\\wwwroot\\htdocs\\images\\"
$IMAGE_DIRECTORY = "/pathto/imagefiles/";

###Nothing below this line should need to be configured.###
my $line = "";
my $file1= "";
my $x=0;
opendir (DIR,"$IMAGE_DIRECTORY");
while ($fileName = readdir(DIR)) {
next if ($fileName !~ /\w/);
$ls[$x] = $fileName;
$x+=1;
}
closedir(DIR);
srand();
$file1 = int(rand(@ls));
print ("Pragma: no-cache\n");
if ($ls[$file1] =~ /\.gif$/i) {
print ("Content-type: image/gif\n\n");
} elsif ($ls[$file1] =~ /\.jpg$|\.jpeg$/i) {
print ("Content-type: image/jpg\n\n");
} elsif ($ls[$file1] =~ /\.png$/i) {
print ("Content-type: image/png\n\n");

} else { #Not a gif or jpeg file. Exit the program.
exit(0);
}
open (INPUT, $IMAGE_DIRECTORY . $ls[$file1]);
while ($line = ) { #Print the image out to the browser.
print ($line);
}
close(INPUT);
exit(0);

#FILE: random.pl
#####################################

Leave a Reply

Your email address will not be published. Required fields are marked *