Easy ways to rotate 5 or so pictures on a website

DarrylLicke

Member
Nov 13, 2001
174
0
0
Any ideas? I think it can be done with a script (either perl or PHP but I have no PHP experience) but i just need to clarify. Or if there is an easier way please say.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: DarrylLicke
Any ideas? I think it can be done with a script (either perl or PHP but I have no PHP experience) but i just need to clarify. Or if there is an easier way please say.

chron and a dirt simple sed command to change the image name in the html file.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Here's one way.
Say I want to rotate the images once/hour. They are named:
image0.jpg
image1.jpg
image2.jpg
.
.
.
image23.jpg

Take your html file, myfile.html, and rename it myfile.temp. Edit the .temp file to replace the image name with the string ROT_IMAGE.
Now put the following command in your chrontab to run every hour:

sed 's/ROT_IMAGE/image'$(date +%H)'.jpg/' myfile.temp > /path-to-html-dir/myfile.html

This will replace the string ROT_IMAGE with a file name formed with the hour of the day, and write the result in your html directory.
 

DarrylLicke

Member
Nov 13, 2001
174
0
0
Originally posted by: ergeorge
Here's one way.
Say I want to rotate the images once/hour. They are named:
image0.jpg
image1.jpg
image2.jpg
.
.
.
image23.jpg

Take your html file, myfile.html, and rename it myfile.temp. Edit the .temp file to replace the image name with the string ROT_IMAGE.
Now put the following command in your chrontab to run every hour:

sed 's/ROT_IMAGE/image'$(date +%H)'.jpg/' myfile.temp > /path-to-html-dir/myfile.html

This will replace the string ROT_IMAGE with a file name formed with the hour of the day, and write the result in your html directory.

Was that in perl? if so you lost me which means you will defintely lose the client who has to upkeep the site after I'm gone. Coould you explain it again?

 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: DarrylLicke
Was that in perl? if so you lost me which means you will defintely lose the client who has to upkeep the site after I'm gone. Coould you explain it again?

Obfuscation = Job Security :D
Sorry, just kidding!

Nope, no perl.
Let's start with the basics.

chron is a service that lets you execute specified commands at regular, specified interval. If you want to rotate your images based on time, chron will almost certainly be involved.

sed = stream editor
While not it's only purpose in life, sed is nice for doing search/replace on text files.

Check out the man pages on chron & sed for more information.

the command I gave:

sed 's/ROT_IMAGE/image'$(date +%H)'.jpg/' myfile.temp > /path-to-html-dir/myfile.html

is a simple sed script. Let's dissect a bit:

's/ROT_IMAGE/image'$(date +%H)'.jpg/'
This tells sed to replace all occurances of the string ROT_IMAGE with a string of the form image$(date +%H).jpg
The $(date +%H) will evaluate to the current hour on a 24 hour clock. So, at 8:05 a.m., ROT_IMAGE would be replaced with image8.jpg
This is just an example. There's lots of ways you could form that new file name.

myfile.temp:
This is identical to your html file except all references to the image file that you want to rotate have been replaced with the string ROT_IMAGE sed operates on this file, replacing all the occurences of ROT_IMAGE with the new file name as described above and sends the results on to stdout

> /path-to-html-dir/myfile.html
This redirects the output of stdout to the html file that you are actually publishing. So, everytime you run this script, it replaces the html file in the server directory with the .temp file via sed.

I'm sure there are lots of ways to do this. Probably cleaner then this. For instance. I don't think the .temp file should be neccesary, but I don't recall how to have sed write to the same file it's reading from (ie. modifying it in place.). Just my rough cut.



 

kt

Diamond Member
Apr 1, 2000
6,032
1,348
136
How often do you want to rotate the images? Do you want to rotate the images on every page refresh? Every hour? It can be easily done with PHP by storing the image filename in an array. Another question is, will the rotation be fixed or random?