BubblesSOC

Hi I'm Bubs, welcome to my home on the web!

Thanks for stopping by! You have stumbled upon the personal website of Sidney Collins (aka Bubs). I'm a 24-year old student at the University of Alabama studying Computer Science and Mathematics. When I'm not busy programming you'll probably find me writing, drawing, reading, shopping, eating, or playing Monopoly.

Read More


Aug 5

PHP Avatar Rotation Revisited

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 :)

Post Info

Bubs posted this entry 3 months, 2 weeks ago on Tuesday, August 5th, 2008 at 12:55 pm.


Comments So Far

Only one person has commented on the post “PHP Avatar Rotation Revisited.” Why don't you leave a comment?

  1. Kristin's Gravatar

    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

Leave a Comment

Leave a Comment

Name, email, and comment required. Email never displayed. Upload a Gravatar to be displayed with your comment. Comments containing links will be moderated for spam prevention.

« Patchwork Clutch
Another Showcase Update »

Search

Search the Archives

Hop to the Top