Aug 5
A few of my readers emailed me requesting an image extension rather than a .php extension for my PHP Avatar Rotation since some forums require a valid image extension. This can be accomplished if your server has mod_rewrite enabled. I’ve also updated my previous example so that animated .gifs are no longer displayed as static images.
Usage
First, create a folder on your server then upload your favorite avatars to that folder.
Copy and paste the following code into a .php file (you can name the file whatever you want):
<?php
/*******************************************************************************
* PHP AVATAR ROTATOR
* VERSION 2
*******************************************************************************/
$path = "/images/avatars/"; // Path to your avatar folder relative to the root directory of your site
/******************************************************************************
* DO NOT EDIT BELOW THIS LINE!
******************************************************************************/
$dir = $_SERVER['DOCUMENT_ROOT'].$path;
$avatars = array();
// Open avatar directory and read its contents into an array
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (filetype($dir.$file) == "file" && getimagesize($dir.$file)) {
array_push($avatars, $file);
}
}
closedir($dh);
}
}
// Create random avatar
$img = $dir.$avatars[rand(0, count($avatars)-1)];
$info = getimagesize($img);
if ($info[2] == 2)
header('Content-Type: image/jpeg');
elseif ($info[2] == 3)
header('Content-Type: image/png');
else
header('Content-Type: image/gif');
readfile($img);
?>
Be sure to change $path (on line 7) to the appropriate value (the path to your avatar folder relative to your site’s root directory).
After you upload the .php file you created to your server, the avatar rotation is available for use. In your forum’s profile control panel, type the URL of the file in the appropriate field. Example URL:
http://bubblessoc.net/images/rotator.php
Image Extension
If you want to use an image extension with your rotation, you need to create/edit an .htaccess file. The .htaccess file must be located in the same directory as the .php file you uploaded. For example, I uploaded my avatar rotation (rotator.php) to the directory /images, so I created an .htaccess for the /images directory.
Creating an .htaccess file is simple. Open a text editor. Create a new file. Save the new file as .htaccess. Notice that .htaccess has no filename, only a file extension!
Paste the following into your new/existing .htaccess file:
RewriteEngine On RewriteRule ^rotator.*$ rotator.php
Be sure to change rotator to the filename of your avatar rotation! Then save and upload. You can now access your avatar rotation with an image extension!
Real URL:
http://bubblessoc.net/images/rotator.php
URL with Image Extension:
http://bubblessoc.net/images/rotator.gif
http://bubblessoc.net/images/rotator.jpg
http://bubblessoc.net/images/rotator.png
Notes
- Only .gif, .jpg, .png images allowed.
- Other than making sure that the file is a valid image, this example does not check for errors.
- Check to make sure that your forum allows avatar rotations before using
Kristin (Quote)
Great rotation! I might try this out myself.
I wish I could write scripts lol, not that talented!
Tue Aug 5th, 2008 1:04 pm