Here is the script I use to do the avatar rotation. It uses the caching function we put into SMF 2.0 so it doesn't have to recreate the file list everytime. If you want to use it without SMF all you have to do is define your own cache_get_data and cache_put_data functions. cache_get_data should return null if the cache doesn't exist or it is expired.
Essentially what the cache_ functions do is add a timestamp and serialize the data and put it into a file. The read part looks for the file and reads it and then check the timestamp to see if it is still good.
I think some of the code is not used anymore since it now just redirects insteads of trying to create the image.
<?php
require_once('../forum/SSI.php');
$url = 'http://www.geekoverloaded.com/avatars/';
$dir = dir(dirname(__FILE__));
$imgtypes = array(
'.png' => 'imagecreatefrompng',
'.jpg' => 'imagecreatefromjpeg',
'.gif' => 'imagecreatefromgif',
);
if (($imgdata = cache_get_data('avatar_rotation_image', 180)) == null)
{
$imgs = getFiles();
$img = $imgs[rand(0, count($imgs)-1)];
$imgsize = getimagesize(dirname(__FILE__) . '/' . $img);
$imgdata = array($img, $imgsize);
cache_put_data('avatar_rotation_image', $imgdata, 180);
}
else
list ($img, $imgsize) = $imgdata;
$ext = substr($img, -4);
if (isset($_REQUEST['directory']))
{
echo '
<table>
<tr>
<td>File</td>
<td>Image</td>
</tr>';
if (!isset($imgs))
$imgs = getFiles();
foreach($imgs AS $image)
{
echo '
<tr>
<td align="left">', $image, '</td>
<td align="center" valign="middle"><img src="', $url, $image, '" alt="', $image, '" /></td>
</tr>';
}
echo '
</table>';
exit;
}
header("Location: " . $url . $img);
function getFiles()
{
global $imgtypes;
// Try to shortcut
if (isset($_REQUEST['recache']) || ($imgs = cache_get_data('avatar_rotation', 43200)) == null)
{
$imgs = array();
$dir = dir(dirname(__FILE__));
while (($entry = $dir->read()) !== false)
{
$ext = substr($entry, -4);
if (isset($imgtypes[$ext]))
$imgs[] = $entry;
}
$dir->close();
cache_put_data('avatar_rotation', $imgs, 43200);
}
return $imgs;
}
?>