Perl Script to display Image

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am not sure if this is the correct forum for this question. If not,
perhaps someone could point me in the right direction.

FreeBSD-6.2
Apache-2.2.4_2
Perl v5.8.8

The following Perl script is suppose to place a new image in a web page
on a daily basis. For some reason, the image is not displayed. Only the
famous red [X] is present. When I run the script from the command line,
it does display the correct file name.

I have a very basic configuration file. Should I be making some other
changes to it, or do I need to load a module to get this to work
correctly.

This is the Perl script (commented):

To display an image simply use this in your HTML:
<img src="location_of_your_perl_script.pl">

#!/usr/local/bin/perl

# find out the day of the year
my $day_of_year = (localtime(time()))[7];

# define the path where the images live "." is the current directory
$path = ".";
# read all the jpg, gif or png filenames from the directory into an
array opendir(DIR, $path);
@files = grep { /\.(jpg|gif|png)$/i } readdir(DIR);
closedir(DIR);

# sort the filenames alphabetically
@files = sort( {lc $a cmp lc $b} @files);

# count the number of images
$no_of_images = scalar(@files);

# Now the fun bit :) We loop through the images once before
# repeating them in the same order. If we divide the current
# number of day of the year by the number of images in the
# directory we get the number of times have repeated the images.
# We are interested in the remainder of this calculation (this
# is calculated using the % operator). Note - there must be
# less than 365 images in the directory! We need to subtract
# one from this number because arrays start at zero not 1!
if ( $no_of_images <= $day_of_year ) {
  $image_to_use = ($day_of_year % $no_of_images)-1;
}
else {
  $image_to_use = $day_of_year-1;
};
print "Location: $files[$image_to_use]\n\n";

Again, I apologize if this is the wrong forum to ask this question on.


-- 
Gerard

A casual stroll through a lunatic asylum shows that faith does not
prove anything.

	Friedrich Nietzsche

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux