PHP Avatar Rotator
Ive you havent noticed, my avatar rotates on each page.
Heres a bunch of code that you can use to get the same effect.
Before I start theres a few things I need to point out.
- I didnt create the code (http://bubblessoc.net/archives/php-avatar-rotation-revisited/)
- You will need a web server
- This rotator is used by MyBB's MattR
Ok so heres how to do it.
- - - - - - - - - - - - - - - - - - -
- First things first you need to create a new textdocument.
- Once opened paste the following code into it.
PHP:
<?php
/*******************************************************************************
* PHP AVATAR ROTATOR
* VERSION 2
*******************************************************************************/
$path = "/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);
?>
- Just save it as avatar.php, making sure after you have saved it that it is an actual PHP file and not avatar.php.txt
- After you have create the php file, create a new folder called avatars
- In this folder you will have all the avatars you want to rotate.
You really want to have all image dimensions exactly 100px x 100px. Alot of sites dont allow any bigger than that and some resize them but some do not. And if they do and your image isnt 1:1 then the image will be squashed to 100px x 100px. So its in your interests to resize it
- After that upload everything we have just created to the ROOT of your webserver.
- After everything is uploaded then you want to check it by typing in this link. Obviously changinging "yoursite.com" to your web url.
http://yoursite.com/avatar.php
- Hit refresh a few times and the avatar should change each time.
- Now all you have to do is go to your UserCP, along the left handside click change avatar. Then where it give you the option to upload the avatar by url, paste that url in
- Hit save and thats it.
Good luck