Random avatar...

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
AGI1122

Random avatar...

Post by AGI1122 »

Whooppeee, I figured out how to have random avatars. Refresh this page and watch my avatar, a different one can appear at random. There is 8 avatars in all. :)
K.J.

Re:Random avatar...

Post by K.J. »

Kewl.

Is this done with PHP?

K.J.
AGI1122

Re:Random avatar...

Post by AGI1122 »

Yeah, I still need to refine it, so the randomizer will work better... but here is the source:

Code: Select all

<?php
$avatars = array("0","1","2","3","4","5","6","7");
$random = array_rand ($avatars, 1);
$directory = "http://www.agigames.com/avatar";

if ($random == "0") {
  $avataropen = "$directory/agi.gif";
}
if ($random == "1") {
  $avataropen = "$directory/death.gif";
}
if ($random == "2") {
  $avataropen = "$directory/blue.gif";
}
if ($random == "3") {
  $avataropen = "$directory/idiot.gif";
}
if ($random == "4") {
  $avataropen = "$directory/idiot2.gif";
}
if ($random == "5") {
  $avataropen = "$directory/skull.gif";
}
if ($random == "6") {
  $avataropen = "$directory/stupid.gif";
}
if ($random == "7") {
  $avataropen = "$directory/homer.gif";
}
if ($random > "7") {
  $avataropen = "http://www.agigames.com/avatar.gif";
}

$avatar = fopen("$avataropen", "w+");

while (!feof ($avatar)) {
  print fread($avatar,120);
}
?>
AGI1122

Re:Random avatar...

Post by AGI1122 »

Well I have finshed the avatar uploader completely. It checks a directory at http://www.agigames.com/avatar/avatars/ and adds all the avatars that are in the directory to the random array. Then it picks one out of the array and displays it.

I don't have to name the avatars anymore in my source code, all I do is upload more avatar when I want them to appear.

Code: Select all

<?
$absolute_path = "/home/agigames/public_html/avatar/avatars";
$directory = "http://www.agigames.com/avatar/avatars";
$dir = opendir($absolute_path);
while($avatar = readdir($dir)) {
  if (($avatar != "..") and ($avatar != ".")) {
    $avatars[] = "$avatar";
  }
}
$random = array_rand ($avatars, 1);
$randomAvatarURL=$avatars[$random];
$avatar = fopen("$directory/$randomAvatarURL", "w+");
while (!feof ($avatar)) {
  print fread($avatar,120);
}
?>
Post Reply