Good random image script reposted

I posted the message below to the programming board, but haven’t gotten a response. Maybe this is a better place for it?:


I am looking for a good random image generator and haven’t had much luck with google. The problem is that integrating any sort of script into my website is very new to me, with the exception of running Greymatter for my photolog (yeah, I know, I don’t have to actually touch code).

I have a place on the front page set aside for an image and a folder full of images of the same dimentions. Now I just need something to tie the two together. I want a different image displayed everytime the page is loaded, but not a slideshow or anything else too heavy.

A mockup of the page will be at if it ever finishing uploading.The big rectangle below the top banner is where the image will be placed. I’ve placed a clear gif over it as a link to the gallery page.

Thanks in advance for any suggestions/links/snippets

Matt’s Random image is a pretty good solution. You can find it here

If you have specific questions about how to set it up, ask away.


Thanks so much! All of the scripts I could find on my own were these clunky JS scripts that required huge chunks of code (if I wanted the array to include 20 or 25 images). This looks like a good, solid script with really striaght forward instructions.

I can’t seem to make it work and I don’t have a clue as to why. After a couple of hours, I went ahead and made a very basic page to remove all of the variables (not code variables, but what-could-be-messing-up variables).

a text version is uploaded at

I changed the file extention to .cgi because that’s how it was set on the demo page and, well, I’d tried everything else.

I am beginning to suspect that it has something to do how I uncompressed it. Using the default settings (WinZip for .zip and PowerArchiver for .tar.gz), the .pl file opened in notepad with a bunch of unrecognized-charachter-rectangles floating around, which I converted to line breaks before modifying and uploading.

To answer the question of someone on the other post, I have -0- coding experience for the same reason that I do not golf. There are just some things I do not do well.

Are the permissions correct for the .cgi/.pl file as I recall they must be executable with a setting of at least 755.
Also did you upload the .cgi in ascii mode?

I had a couple of problems recently with cgi files not uploaded correctly. I got around it by SSH’ing in and editing the .cgi. I just added a character and saved the file then reopened it and removed the character and saved it back to it’s original.


(I’m reposting this PHP script I left in the Programming thread.)

Seeing that you have no coding experience, this ultimately may be of little help for you.

What generates your home page? A script? Or is it a static page?

If it’s PHP, you can use this simple, inefficient random image Frankenscript. (I chopped out some of my site-specific info, but this ought to do the job.)

No specific file-naming conventions and no need to change the script as new images are added. But it is inefficient.



function make_seed() {
list($usec, $sec) = explode(’ ', microtime());
return (float) $sec + ((float) $usec * 100000);

/* Define current dir */
$current_dir = ‘images/dirname’;

$RAND_MAX = mt_getrandmax();

$count = 0;

/* Open current local dir for reading /
$cd_handle = opendir($current_dir);
While we find files and dirs in the current dir… */
while (($file = readdir($cd_handle)) !== false) {

/* Check for dot files, so we don’t use .htaccess and ./… dirs */
if (substr($file, 0, 1) != ‘.’ && $file !=‘index.php’) {
$filearray[$count] = $file;

/* Close the open dir */

$rand = mt_rand();
$randval = intval( ( $rand / $RAND_MAX ) * $count );
$file = $filearray[ $randval ];

print ‘’;

?>[/code](I made a minor edit to the PRINT line for this repost, as well.)

Everything between the start and end script tags would go into a text file and saved as the name of the script to call. The file would need execute permissions and the variable ‘$current_dir’ would need to be edited to point to the directory where you save all of your image files.

I use this on my own page here to pull up a random image from a directory full of them each time the main frame reloads. (I have a few minor tweaks as well, since I make the image itself a link to the days’ comic that I’m loading, but I pulled that detail out to simplify the script for you.)

I currently have about 240 images in that image pool.

Thanks everyone.

And yes, the file is chmod’ed correctly. I really think that the problem is related to how I’m uncompressing the file, but I’ll look into it. This route from Ardco is going to undergo a little investigation.

Yeah, no clue. SSH? Wget? I’m sure google or the Dreamhost Wiki knows. If not, I’ll be back for another round.

I just wanted to check in b/c the week has started, which means I’ll be MIA.

Okay I’ve just tried installing this script on a test page and have it working okay.
Initially I used ftp (well sftp) to copy over the file and then copied some gifs into a newly created images directory.

Initially I just got a blanks page so I did as I mentioned earlier and used SSH to open and save the .pl file and that got it working okay.

Just as an afterthought I’ve installed it a second time at another directory but this time used ftp i.e. not secure ftp (sftp) and that appears to have left the .pl file alone.

If it helps…

I created a directory called test and switched to ‘test’.
I created a directory called ‘images’ for the gifs and then created an index.html file with the single line.

In the .pl file I had:

$basedir = “”;

@files = (“images/gif1.gif”,“images/gif2.gif”,“images/gif3.gif”,“images/gif4.gif”,“images/gif5.gif”);

Then just changed the permission on the .pl file to 755.

Hope that helps. Replace with your domain name.

P.S. You don’t need to worry about putting the .pl file into a cgi-bin directory. Stick it anywhere. To start with you might want to stick everything in the same place. :slight_smile:


hey a while ago when I ran a radio station I needed a random image script.

Anyway I got one built up here

<?php $r_image[]="no1.png"; $r_image[]="no2.png"; $r_image[]="no3.png"; $r_image[]="no4.png"; $r_image[]="no5.png"; $r_image[]="no6.png"; $bgpng = $r_image[ rand(0,5) ]; include("includes.php"); header("Content-Type: image/png"); $im = imagecreatefrompng("$bgpng"); $bgc = imagecolorallocate ($im, 44, 44, 224); $tc = imagecolorallocate ($im, 134, 13, 255); $tc2 = imagecolorallocate ($im, 0, 0, 204); $tc3 = imagecolorallocate ($im, 204, 0, 0); $font = "F:/xampp/htdocs/fonts/arial.ttf"; $black = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle ($im, 0, 233, 150, 30, $bgc); ImageTTFText ($im, 8, 0, 8, 10, $black, $font, "Now Playing: ".$current_song); ImageString($im, 5, 50, 25, "Total Listeners: ".$listeners, $tc3); ImageString($im, 5, 250, 22, "Max Listeners:" .$max_listeners, $tc2); ImageString($im, 5, 20, 38, "Join us for some fun, it will be cool.", $tc); ImageString($im, 10, 10, 55, "Brought to you by DGCT Radio. We do live pranks!" .$desc, $tc2); ImagePNG($im); ?>

This is a pretty good one to use, all you have to do is change the file names to whatever u need them for and take out the font.

It should work, I am not going to say it will cause i havent tested it… but you should be able to include it.

But if you want font then you should be ok.

This script was made over 2 yrs ago… use edit and whatever at your own risk.