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


Jan 15

PHP Avatar Rotation

Have you ever noticed on forums that some users’ avatars will be different each time the page reloads? I recently decided that I wanted to display multiple avatars on Snark, so I sat down and wrote this little script. (This method only works for forums that allow off-site avatars.)

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
*******************************************************************************/

$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');

	$img = imagecreatefromjpeg($img);
	imagejpeg($img);
}

elseif ($info[2] == 3) {
	header('Content-Type: image/png');

	$img = imagecreatefrompng($img);
	imagepng($img);
}

else {
	header('Content-Type: image/gif');

	$img = imagecreatefromgif($img);
	imagegif($img);
}

imagedestroy($img);
?>

Be sure to change $path (on line 6) 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

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

Click here to see my avatar rotation :spin:


Post Info

Bubs posted this entry 6 months, 1 week ago on Tuesday, January 15th, 2008 at 12:58 pm.


Comments So Far

15 people have commented on the post “PHP Avatar Rotation.” Why don't you leave a comment?

  1. Hev's Gravatar

    Hev (Quote)

    Oh my, I love it. Cool script, Bubs. You are one talented woman. I love your fortune cat avatar. Did you draw that one? You did a very good job on it.

    Tue Jan 15th, 2008 1:27 pm

  2. Robmarie's Gravatar

    Robmarie (Quote)

    Bubs! The rotation is so shiny…. I can’t stop reloading the page! =P

    Tue Jan 15th, 2008 4:40 pm

  3. Chien Yee's Gravatar

    Chien Yee (Quote)

    Your avatars are so cute xD *refreshes non-stop*

    Tue Jan 15th, 2008 4:55 pm

  4. Rilla's Gravatar

    Rilla (Quote)

    There are some forums which do not recognise .php as a valid image location. It’d be helpful if you can change the code a bit so that you are able to access the rotation script with .jpg/gif/png.

    Tue Jan 15th, 2008 5:14 pm

  5. mendifae's Gravatar

    mendifae (Quote)

    very cool bubs!! I like your little example graphics too! You amaze me with your coding skills! :)

    Tue Jan 15th, 2008 6:22 pm

  6. Brent's Gravatar

    Brent (Quote)

    Such a handy little script. I’ve use a variation of it before to display random photos before I discovered the magic that is Flickr <3 Thanks for sharing it though, it will come in handy :)

    Wed Jan 16th, 2008 12:57 pm

  7. Christine's Gravatar

    Christine (Quote)

    Bubs. You are my hero. Seriously. I totally admire that you can just whip out a little script out of nothing!! I don’t currently troll any forums right now. but the moment I do, I’m so using this! <333

    Wed Jan 16th, 2008 3:09 pm

  8. Cherry's Gravatar

    Cherry (Quote)

    I remember using an avatar rotation script on a forum a while ago and I got so carried away with it that I had over 200 avatars in the rotation. Hehe

    Thu Jan 17th, 2008 5:19 pm

  9. Ashley's Gravatar

    Ashley (Quote)

    Very cute Bubs! I also refreshed many times to see all of the cuteness. :)

    Fri Jan 18th, 2008 4:45 pm

  10. Arwen's Gravatar

    Arwen (Quote)

    That’s really cool!

    Sun Jan 20th, 2008 12:17 pm

  11. Nanda's Gravatar

    Nanda (Quote)

    Wow, that looks pretty interesting! I’m sure a lot of people will find it handy and fun. But I have enough trouble finding or making one avatar for myself, so I’ll stick with one. :D

    Tue Jan 22nd, 2008 11:00 am

  12. Crystal's Gravatar

    Crystal (Quote)

    Sweet script! I wish I was as smart as you!

    Sat Jan 26th, 2008 1:45 pm

  13. FRutee's Gravatar

    FRutee (Quote)

    Nice script. haha. :P I’ve been wondering how its done. :)

    Tue Jan 29th, 2008 11:25 am

  14. Shivon's Gravatar

    Shivon (Quote)

    That’s a really cool script, I wish I didn’t suck at keeping up with forums.

    <3 Voni

    Thu Jan 31st, 2008 1:31 am

  15. Aisy's Gravatar

    Aisy (Quote)

    Hey there. I LOVE your theme. It’s very unique and neat! and cute too. and, how do you make your tabbed navigation work? I have tabs on my site but I couldn’t get them to work but I’ll get on that later when I’m sane enough. hehe…

    and man, you’r so good with PHP.

    Mon Feb 4th, 2008 10:52 am

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.

« Calvin and Hobbes
Dear Pen-Pals »

Search

Search the Archives

Hop to the Top