Page 1 of 1

Avatar PHP

Posted: Wed Oct 30, 2002 6:23 am
by juncmodule

Code: Select all

<?php

function Get_Image_list($dir) {
if(!$dir) {
  $dir = ".";
}
$file_array = array();
$dir_handle = opendir($dir);
$a = 0;
while($file = readdir($dir_handle)) {
if((preg_match('/jpg/',$file)) || 
   (preg_match('/png/',$file)) || 
   (preg_match('/gif/',$file)) ||
   (preg_match('/jpeg/',$file)))  {

   $file_array[$a] = $file;
   $a++;
}
}

return $file_array;
}

$files = Get_Image_list($dir);

$max = count($files)-1;
srand((double)microtime()*1000000);
$index = rand(0,$max);
$image_name = $files[$index];

?>
<img border=0 alt="random image" src="<? echo "$dir/$image_name";?>">

I pulled this from evil walrus so I could study it for my random webpage stuff...then I got impatient and just decided to try to use it for my random avatar...no it will display on my page but, not on the board...

-junc

Re:Avatar PHP

Posted: Wed Oct 30, 2002 6:27 am
by AGI1122
That is because this script isn't designed correctly to work. It would take a rewrite to work properly in an avatar field. To be specific, you can't put a img tag inside an image tag or a url. For instance that script is printing out <img src="url">, but message boards put the url inside of the image tag... so in other words what that script is creating is an image tag inside of an image tag like this:

Code: Select all

<img src="<img src="url">">
which is the problem with this script, but even putting just the url will not work, you have to open and dump the contents of the image file.

Here is my script:

Code: Select all

<? 
$absolute_path = "/home/yoursite/public_html/avatars"; 
$dir = opendir($absolute_path); 
while($avatar = readdir($dir)) { 
if (($avatar != "..") and ($avatar != ".")) { 
   $avatars[] = "$avatar"; 
} 
} 
$avatars = str_replace(chr(32),"%20",$avatars); 
$random = array_rand ($avatars, 1); 
$randomAvatarURL=$avatars[$random]; 
$avatar = fopen("$absolute_path/$randomAvatarURL", "r"); 
while (!feof ($avatar)) { 
        print fread($avatar,120); 
} 
fclose($avatar); 
?>

Re:Avatar PHP

Posted: Wed Oct 30, 2002 8:58 pm
by juncmodule
It took me a while, but, I got it working...eperfect kept on going down on me...Anyway, thanks for the script Chris...It's a lot easier to understand than the one I got from evil walrus. Now, using the same idea I am going to try to make a random script to grab HTML pages in the same way that does....it should be interesting.

Re:Avatar PHP

Posted: Thu Oct 31, 2002 8:26 am
by AGI1122
Alright for those who are using my avatar script, I have something to say, the avatar directory must not contain anything BUT avatar images, the avatar script itself is not supposed to be in the directory either. My script doesn't check to see if the file is an image or if it is not... so your avatars will display x's if there is a file in your avatar directory that is not an image(including my script).

I noticed both juncmodule and Kon-Tiki's show x's which means that you have something in the avatar directory that isn't an image file.

Re:Avatar PHP

Posted: Thu Oct 31, 2002 8:46 am
by juncmodule
That's odd...I see the x's but, I don't have anything extra in that directory...all of the images I do have in the directory display properly. I will have to wait and log on at home and see if I can find out what is causing that...

I think eperfect got mad at me...I crashed the server with that script (had the directory wrong). Opps ;D

-junc

edit: actually, if you go here http://www.juncmodule.com/avatar you can see the files in the directory...

Re:Avatar PHP

Posted: Thu Oct 31, 2002 9:08 am
by AGI1122
Make sure that all 3 of them are uploaded in binary mode. Other than that... not sure why it would be messing up.

Also I built a new avatar script last night... much better than my first because this one allows me to choose which extensions are allowed, plus an option to choose your avatar that you want displayed in the url. If you would be interested in the new script just ask.

Re:Avatar PHP

Posted: Thu Oct 31, 2002 11:04 am
by juncmodule
When you say choose extension...do you mean that I could use it for .HTML too? Either way, I would be interested in at least checking it out. I'm not sure when I'm going to have time to create my own scripts. I haven't even finished my scratchpad yet...all I have to do is throw in a few functions and it's done...blah, maybe this weekend.

By the way, how long have you been programming with PHP?

I will upload all of my images again in binary when I get home just to make sure.

Thanks for all the help Chris!

later,
-junc

Re:Avatar PHP

Posted: Thu Oct 31, 2002 11:44 am
by AGI1122
By extension I mean it doesn't check the file type, it will load anything and everything that is in the avatar directory even if it isn't a image file.
By the way, how long have you been programming with PHP?
Since April 18th, 2002.

Re:Avatar PHP

Posted: Sat Nov 02, 2002 8:13 am
by juncmodule
Hey Chris, when you go to www.juncmodule.com/avatar.php and refresh enough times at one point it displays the text "Options All". That is why my avatar displays a blank from time to time. Do you know what could be causing that?

-junc

Re:Avatar PHP

Posted: Sat Nov 02, 2002 11:32 am
by AGI1122
Hmmmm, well you could try looking for and censoring that text in the script.

Re:Avatar PHP

Posted: Sat Nov 02, 2002 12:48 pm
by juncmodule
ARGH!!! There WAS a file in there...It was hidden, couldn't see it through the URL or FTP, but, I could see it through the cpanel! It appears to be fixed...so yay!