I've been asked several times how do I have my avatar images rotate.
The answer is custom server side scripts.
I have an old way and a new way.
The old way:
Download index.phpLanguage: php
<?php
$images = array();
$dir = opendir('.');
while (($file = readdir($dir)) !== false)
{
if (substr($file, -4) == '.png') { $images[] = $file; }
}
closedir($dir);
if (!isset($_GET['list']))
{
$filename = $images[rand(0, count($images)-1)];
header('Content-type: image/png');
header('Cache-Control: no-cache, must-revalidate, no-store');
header('Content-Length: '.filesize($filename));
readfile($filename);
exit;
}
header('Content-type: text/html; charset=utf-8');
header('Cache-Control: max-age=2592000');
echo "<html><body>\n<table>\n<tr>";
$i = 0;
foreach ($images as $image)
{
if ($i == 4)
{
$i = 0;
print "</tr>\n<tr>";
}
echo '<td><img src="'.$image.'"></td>';
++$i;
}
echo "</tr>\n</table>\n</body></html>";
Place in a directory named avatar.png on your PHP enabled server, along with all the PNG images you want.
Example:
http://zsnes.sourceforge.net/avatar.png
Image will change every request.
To see all images:
http://zsnes.sourceforge.net/avatar.png?list
This method has several drawbacks.
1) Overhead of using PHP to transfer files
2) It seriously destroys caching, increasing load to your server
3) (Large) Images can't be transferred in chunks / multiple requests
4) Entire directory needs to be scanned for every hit
5) No two users see the same image at the same time
6) The HTML sucks
7) Silly directory tricks force loading redirects
8) Static code is forced to generate dynamically
Basically it works for the simple cases, but doesn't scale well, and could potentially kill your server. Some of the drawbacks can be fixed with a more robust PHP script, but PHP isn't the right solution for this. This method also won't work with stricter forums that don't like following redirects, and enforce the file name to end in a known image extension.
The new way:
Create a backend within the server's native URL handling logic.
For direct hits to "avatar.png", have the server generate a random number 0...X where X is one less than the amount of images you have. The avatar images should be numbered. The server responds directly with this PNG file internally.
Since the server itself and not some scripting language on top is handling the file, all file features are available, including range responses and all. It means no scripting language overhead. This could be somewhat achieved in a scripting language with one of the various sendfile extensions various servers have.
Having the random number based on the time, say amount of minutes / 5, users will see the same thing for 5 minute intervals. Along with having "Cache-Control: max-age=300", your server no longer has to be hammered. A user seeing your avatar several times in the same thread or quickly jumping page to page only requests the image once instead of many times. And if he spends his time reading the page before going to the next one, the avatar changes which is the desired effect.
The list page is a static HTML page regenerated each time a new image is added. Thus causing no extra overhead. All pages and images hide being a single URL structure using the server's URL logic. All this with a proper XHTML 1.1 page of course.
Example:
http://nach.nachsoftware.org/avatar.png
List page:
http://nach.nachsoftware.org/avatar.png/list.html
This time, there are no directory redirects.
The superior method does have some drawback though:
1) Different configuration for each server
2) You have to manually add a new instruction for each new avatar set you create (unless you employ wildcards)
3) Cheap hosting providers normally don't let you interface with the server at this level, forcing you to choose a variation of the old clunkier method