Alex on Camel, Egypt
Butchers in Moshi, Tanzania
Remarkable Rocks, Kangaroo Island
Summer Palace, China
Dorota on Brighton Beach

HTML Random Image Script

Well I wanted to create a random image display for one of my sites, but all I could find were JavaScript random image scripts, the problem with the ones I found were that the script had to contain all the list of images, it had to be in the body tag too so couldnt intergrate the Java Script and the image list into an external file.

I had a good look for PHP random image scripts, but nothing as simple as I wanted, so I decided now was the time to brush up on my perl and create my own.

#! /usr/local/bin/perl -w
$basedir = "http://www.code-d.com/papa-smurf/random/";
@files = ("strip01.jpg", "strip02.jpg", "strip03.jpg",);
$num = rand(@files);
print "Location: $basedir$files[$num]nn";

All you need to do to use this script is obviously call it something like randomimage.pl, set your absolute path to Perl, and then stick it in your cgi-bin and set the permissions to 755.

All you need to do to use this script is obviously call it something like randomimage.pl, set your absolute path to Perl, and then stick it in your cgi-bin and set the permissions to 755. To call a random image you just reference it like a normal image.

All you need to do to use this script is obviously call it something like randomimage.pl, set your absolute path to Perl, and then stick it in your cgi-bin and set the permissions to 755. To call a random image you just reference the script like you would for a normal image. If you want to see an example please check out my papa smurf site.

There Are 4 Responses So Far. »

  1. No need for that srand() line in any release of Perl from the past five years. In fact, it destroys the even better randomization that’s already built in.
    Perhaps the code you show is cargo-culted from a decade ago, when that was indeed good advice. Perhaps you should ignore most of the rest of the code you got from the same place. {grin}

  2. No need for that srand() line in any release of Perl from the past five years. In fact, it destroys the even better randomization that’s already built in.
    Perhaps the code you show is cargo-culted from a decade ago, when that was indeed good advice. Perhaps you should ignore most of the rest of the code you got from the same place. {grin}

  3. Hi Randal, thanks for that i have takent that line out and the images load a bit quicker now too 🙂

    Your right my perl days are nearly 5 years out of date hehe

  4. Hi Randal, thanks for that i have takent that line out and the images load a bit quicker now too 🙂

    Your right my perl days are nearly 5 years out of date hehe

Post a Response

*** I'm sorry but due to the large amount of spam, comments without Gravatars will be automatically deleted ***