A few weeks ago I asked about an easy way to display a random photo including associated metadata. I concluded that existing photo gallery software was too complex for such a simple feature. Instead, Morbus Iff came through with another simple Perl script that does this for me.
The script queries a stored comma-separated file called metadata.txt, grabs a random string of data and then writes out a chunk of HTML/CSS using those applied values. To make things easy, the script writes to an include file I have stuck in my global site template.
I’ve written up simple instructions on how to use this script on your site.
Here is the Perl script. Note that you will need to change line 6 to point to your own CSV file. You will also need to change line 20 to point to the include file you want to write to. Also change the path to your thumbnails directory in line 23.
Next, upload the script to your cgi-bin directory (or any directory you can run Perl scripts from).
Make sure you have a thumbnails directory ready to go and have built a file called image_metadata.txt for the script to read from. All of your thumbanils should be the same height and width.
Inside image_metadata.txt you should have a comma-separated file that looks something like this ($image, $year, $location, $title):
abandoned_house.jpg, 2002, Siberia, Abandoned House arches_building2.jpg, 2002, Malta, Arches on the Water baikal_sunset.jpg, 2002, Siberia, Lake Baikal Sunset
Once you have your thumbnails and metadata file ready, you should try running the Perl script from the command line:
Then, using a web browser, check to see if the include file was rendered properly. If it has, then run the Perl script once again and reload the page. You should have a different picture and metadata. If you get a file not found error, then the script did not run properly and write the file.
Now that you have the Perl script running properly, you will want to set up a cron job so that the include file is processed once an hour. To do this, go to your shell account and create a file called crontab.txt. Inside this file you will want to place the following command:
39 * * * * /usr/bin/perl /full/path/to/rand_image_metadata.pl
This tells the cron command to run that command at the 39th minute of every hour. To read more about configuring cron, read Pair’s tutorial.
That’s it. Pretty simple stuff, but highly effective.
Note: My next task is to build a little web form to update the image_metadata.txt file whenever I have new photos to add to the random pile.
Posted by Cameron Barrett at June 5, 2003 06:07 PM