We've moved. Come and join the fun at scottandrewbird.com.

Random images

The random images at the top of the sidebar are cycled using a simple php script. There are many scripts out there that perform much the same function, I kept this one simply as it was one of the first scripts I wrote in PHP. Hey, I'm a sentimental guy. Here's the script in all its glory. Feel free to take it and and modify in any way you see fit. If you've got any questions or comments on this script, or anything else on this site, feel free to drop me a line. The script itself. Put this in a PHP file with your other scripts. Make a note of where this file lives - you'll need to reference it later.
function ranpic($dir, $pictitle) {

  $width = 140; // change this to suit your own images
  $height = 90; // change this to suit your own images
  $curr_directory = opendir($dir);
  while($filename = readdir($curr_directory))	{ 
  	// only show files, ie names with a '.'
  	if (substr_count($filename,".")>0) {
  		// ignore dirs '.' or '..'
  		if (substr($filename,0,1)!=".") {
  			$piclist[] = $filename;
  		}
  	}
  }
  closedir($curr_directory);
  $picnum = rand(0,sizeof($piclist)-1);
  $pic = $piclist[$picnum];
  print "<img src=\"".$dir."/".$pic ."\" width=\"" 
.$width . "\" height=\"" .$height . "\" title=\"" 
.$pictitle ."\" alt=\"" .$pictitle ."\" /> ";
}
To call this script, you'll need something like the following (on the page where you want the image to appear) :
<?php 
include("../scripts/scripts.php");
echo ranpic("../images/random","another random image");
 ?>
Modify the script location, location of your images and the ALT tag as required.

home | previous entry / next entry

add to del.icio.us | digg it | technorati | blinklist | furl | reddit

See also:

If you enjoyed this post, why not receive future posts via email.

Delivered by FeedBurner