![]() |
|
|
| Subject:
JavaScript to display images
Category: Computers > Programming Asked by: pigskinreferee-ga List Price: $20.00 |
Posted:
02 Apr 2006 05:50 PDT
Expires: 02 May 2006 05:50 PDT Question ID: 714582 |
I am looking for a JavaScript script that will run on a FreeBSD 5.4 OS with Perl 5.8.8 installed. I have a collection of images saved in a directory that I want to display one at a time on a daily basis on a web page. I need a script that could sort the files in a given directory and display the first one in the web page I am creating. After 12 midnight the the script would move the previously displayed image to a new location and display the next image in the directory. The directory structure for the stored images is: /usr/local/www/seibercom/htdocs/images I would like to store the displayed images under the 'image' directory. I am not sure if this can even be accomplished with Java, but I thought I would give it a try. The alternative would be to try and do something with Perl and run it from CRON perhaps. Please contact me if you require further information. | |
| |
|
|
| Subject:
Re: JavaScript to display images
Answered By: palitoy-ga on 02 Apr 2006 08:37 PDT Rated: ![]() |
Hello pigskinreferee-ga,
Thank-you for your question. I look forward to hearing your comments
on my script - I can alter it if it is not exactly what you require.
I wanted to post something here for you to peruse before you reply to
my clarifications.
If you require the images still to be moved I can easily build this in
to the script for you although I think it may not be necessary
depending on your exact requirements and the number of images in the
directory.
You should place the script below into the directory of images on your
server (set the permissions of the script to chmod 755).
When run the script will calculate the current day of the year (the
number of days since January 1st) and the number of JPG images in the
directory. If there are more images in the directory than days in the
year then the script will have to be altered.
If there have been more days in the year so far than there are number
of images in the directory the script will simply begin displaying
each image from the beginning again. The script will loop through
each image in the directory before displaying it twice. The images
are sorted alphabetically.
The script stores the name of each image file in an array and outputs
the image depending on the day of the year.
To display an image simply use this in your HTML:
<img src="location_of_your_perl_script.pl">
If any of this is unclear please take a close look at the script and
ask for clarification. I will try to respond as swiftly as possible.
Likewise if you do still require the images to be moved or anything
else changing please ask for clarification.
#!/usr/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 filenames from the directory into an array
opendir(DIR, $path);
@files = grep { /\.jpg$/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"; | |
| |
| |
pigskinreferee-ga
rated this answer:
and gave an additional tip of:
$5.00
A perfect solution to my question. I am very please with it. It was delivered exceptionally quick also. |
|
| Subject:
Re: JavaScript to display images
From: palitoy-ga on 04 Apr 2006 04:57 PDT |
Thank-you for the 5-star rating, kind comments and tip! They are all appreciated. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
| Search Google Answers for |
| Google Home - Answers FAQ - Terms of Service - Privacy Policy |