![]() |
|
Avatar Rotator - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: PHP (https://sinister.ly/Forum-PHP) +--- Thread: Avatar Rotator (/Thread-Avatar-Rotator) |
Avatar Rotator - TeraByTe - 06-21-2011 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.
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 Code: <?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
![]() RE: Avatar Rotator - slbmeh - 06-29-2011 Here is some slightly more efficient code I wrote real quick. PHP Code: <?php
$imgdir = "images";
$images = scandir($img_path);
$imgpath = $imgdir . DIRECTORY_SEPARATOR . $images[mt_rand(2,count($images))];
header("Content-type: " . image_type_to_mime_type(exif_imagetype($image)));
readfile($imagepath);
?>It eliminates unnecessary loops and tests, uses a more efficient rand function, as well as supports many more image types. You can break this by having something other than an image file in your directory. If the user's browser doesn't support the image it will simply not display. A more documented copy of the previous code follows: Spoiler:PHP Code: <?php
/**
* The location of the directory your images are located.
* Can be absolute or relative.
*/
$imgdir = "PHPDebug";
/**
* Populate our image array with directory contents.
*/
$images = scandir($img_path);
/**
* Generate a random integer based on our image count.
* Using mt_rand() because it is generally faster than rand()
* We set the min to 2 to eliminate the current and parent directories.
*/
$rand = mt_rand(2,count($images));
/**
* Assign our image path.
*/
$image = $imgdir . DIRECTORY_SEPARATOR . $images[$rand];
/**
* Pull the exif data from the fill and
* convert it to mime type.
*/
$mime = image_type_to_mime_type(exif_imagetype($image))
/**
* Push our our header based on exif data.
*/
header("Content-type: " . $mime);
/**
* Output the contents of our image.
*/
readfile($imagepath);
?> |