News:

Join the Facebook Fan Page.

Main Menu

Random Avatar Script

Started by Meriadoc, August 03, 2003, 01:32:29 AM

Previous topic - Next topic

Meriadoc

This gets asked decently often, I've asked about it before and all but it got it working. Or rather, it works wonderfully, only very tweaky.
Here's what I use.
This is rotate.php which i set as my avatar.

<?php
$dir
= opendir('/full/path/to/dir/of/imgs/');
$count = -1;
while (
$file=readdir($dir)) {
 
$i = stristr($file,$userprofile[$mname]['memberName']);
  if(
$i != FALSE) {
     
$image[] = $file;
     
$count++;
  }
}
closedir($dir);
$image = $image[rand(0, $count)];
header('Location: http://www.thepalantir.com/premium/avatars/'.$image);
?>

So it all works fine. I have a folder (premium/avatars) which multipul users put their avatars which are named by memberName_filename.jpg (or gif) THe problem here is that a user who is not me tried to do this with her 3 avatars in this folder. Nothing but the red X (it had worked for me but I had cleared out all my avatars). So I upload stuff under my account to test it. There are now files with her member name and with mine -> user_file1.jpg user_file2.gif etc and Meriadoc_av1.jpg Meriadoc_file2.gif etc. Now all the sudden when you check the posts she's got rotating avatars.... but it's using my images rather than hers! :o I can't figure out why it's calling my memberName. I even logged in under my tester account and it still did that, so it wasn't taking the viewers memberName.

Any ideas?
Merry
If I know the way home and am walking along it drunkenly, is it any less the right way because I am staggering from side to side? : Leo Tolstoy
Everything I know I learned from Calvin and Hobbes.
And patience is about the most useful thing you could ever have.  That and backups. : [Unknown]
If I choose to send thee, Tuor son of Huor, then believe not that thy one sword is not worth the sending. : Ulmo, Lord of the Waters - Unfinished Tales, by J.R.R. Tolkien

Overseer

Well i'm going to tweak this in a little bit to rotate banners :)

..maybe more too ;)

Aquilo

I posted this at the YaBB SE forum but it's just the script made to work and them optimized.


//FULL system PATH to image directory
$d=@opendir('C:\\Inetpub\\wwwroot\\YaBBSE\\YaBBImages\\avatars\\');
  $i=0;
  while($f=readdir($d))
  {
     (stristr($f,'jpg') || stristr($f,'gif')) ? (($_[]=$f) && ($i++)) : $i ;
  }
closedir($d);
///URL to image directory
header('Location: http://localhost/YaBBSE/YaBBImages/avatars/' . $_[rand(0, $i-1)]);

Meriadoc

the problem with that being it doesn't take into account multipul users have files in the same folder:
user1_file1.jpg
user1_file2.gif
user2_file1.gif
user2_file2.jpg
user2_file3.jpg
user3_file1.jpg

etc... so what should be happening is that in each post by one of these users that calls rotate.php as their avatar should use the $username[$mname]['memberName'] variable for that post and pull one of the correct images out. But right now it's taking mine and mine only no matter who's using the file.
Alternitively i could generate a rotate.php file for each user (wouldn't be more than 30 users i wouldn't think so no huge deal) because i found that if i hard code the username into the file it works fine. so somehow it's always using me. i can't find a place where i hardcoded myself in though... other users see my avatars also, so it's not just whoever's looking at it.
odd no?
thanks for help
merry
If I know the way home and am walking along it drunkenly, is it any less the right way because I am staggering from side to side? : Leo Tolstoy
Everything I know I learned from Calvin and Hobbes.
And patience is about the most useful thing you could ever have.  That and backups. : [Unknown]
If I choose to send thee, Tuor son of Huor, then believe not that thy one sword is not worth the sending. : Ulmo, Lord of the Waters - Unfinished Tales, by J.R.R. Tolkien

Aquilo

so if $username[$mname]['memberName'] == Meriadoc
and the image directory looks like

Meriadoc_yabb.jpg
Meriadoc_smf.gif
Meriadoc_yahoo.jpg
Aquilo_yabb.jpg
Aquilo_smf.gif
Aquilo_yahoo.jpg
[unknown]_yabb.jpg
[unknown]_smf.gif
[unknown]_yahoo.jpg

it should only pick the images with your username thats no big problem just look for those too

//FULL system PATH to image directory
$d=@opendir('C:\\Inetpub\\wwwroot\\YaBBSE\\YaBBImages\\avatars\\');
 $i=0;
   while(false !== ($f=readdir($d)))
   {
      (stristr($f, $username[$mname]['memberName']) && (stristr($f,'jpg') || stristr($f,'gif')) ?
      (($_[]=$f) && ($i++)) : $i);
   }
closedir($d);
///URL to image directory
Header ('Cache-control: private');
Header ('Pragma: no-cache');
header('Location: http://localhost/YaBBSE/YaBBImages/avatars/' . $_[rand(0, $i-1)]);


I hope this helps, I have it working but not with $username[$mname]['memberName'] since I'm not shure how your pulling it into another file.
if you need any other help with it lwt me know

Meriadoc

thanks for sticking this out :)

you've got basically what I've got. I've got that username var in there, though I don't use the stristr looking for jpgs of gifs, nothing else can be there except the .php file (the script we are looking at) which shouldn't matter since it doesnt' start with a user's username.
The way I pull the var is simple. I save this code as a file and specify that file as my avatar. In the end I think it works rather like an include. I don't need to global anything...
If I know the way home and am walking along it drunkenly, is it any less the right way because I am staggering from side to side? : Leo Tolstoy
Everything I know I learned from Calvin and Hobbes.
And patience is about the most useful thing you could ever have.  That and backups. : [Unknown]
If I choose to send thee, Tuor son of Huor, then believe not that thy one sword is not worth the sending. : Ulmo, Lord of the Waters - Unfinished Tales, by J.R.R. Tolkien

Mecha Dude

This is what I use, I just dump it in a directory, and off we go!  As for multiple users, I feel just giving them thier own directory is just dandy :P

No this is not mine (hence the credits are intact):


<?php
//////////////////////////////////////////////////////////////////////
/*                  Suho1004's PHP Sig Rotator V2.0                 */
/*                                                                  */
/* This script takes an inventory of all image files in a directory */
/* and displays one at random. All you need to do is save this code */
/* in a directory that contains your images (name it what you will, */
/* but make sure it has a ".php" extension). Then just link to this */
/* script to get your random image.                                 */
/*                                                                  */
/* I would recommend naming this file "index.php," and then you can */
/* just link to the directory itself (like I do in my example).     */
/*                                                                  */
/* Once you've read these instructions, feel free to delete them to */
/* cut down on the file size. I claim no rights to this code, which */
/* means you can do whatever you like with it. Have fun. :-)        */
/*                                                                  */
/* Thanks to TP for the gentle nudging...                           */
//////////////////////////////////////////////////////////////////////

if ($dir = opendir("."))
{
     $list = buildimagearray($dir);
     displayrandomimage($list);
}

// This function reads all the files in the current directory and adds all image files to the array $list[]
function buildimagearray($dir)
{
     while (false !== ($file = readdir($dir)))
     {
          if (!is_dir($file) && getimagesize($file))
          {
               $list[] = $file;
          }
     }
     return $list;
}

// This function selects a random image, determines the mime type, opens the file for reading,
// and then outputs the image
function displayrandomimage($list)
{
     srand ((double) microtime() * 10000000);
     $sig = array_rand ($list);

     $size = getimagesize ($list[$sig]);
     $fp = fopen($list[$sig], "rb");

     if ($size && $fp)
     {
          header("Content-type: {$size['mime']}");
          fpassthru($fp);
          exit;
     }
}
?>

eFishie

My avatar script is only 6 lines! BEAT THAT!

<?
header ("Content-type: image/png");
$count=6;
$img=rand(1,$count);
$todisp="$img.PNG";
$disp = imagecreatefrompng ($todisp);
imagepng ($disp);
?>


Of course, it has a couple disadvantages, like having to name the files in numerical order, but it's useable.
--Jon

Abstraction Point Electronic Music Community - We don't bite! Really! I promise!

Check out my newest song: Alive

[Unknown]

#8
Can and will.

<?php
header
('Content-Type: image/png');
imagepng(imagecreatefrompng(rand(1, 6) '.PNG'));
?>


Or...
<?php
header
('Content-Type: image/png');
@
readfile(rand(1, 6) '.PNG'));
?>

(second doesn't require GD, and is probably faster.)

-[Unknown]

eFishie

--Jon

Abstraction Point Electronic Music Community - We don't bite! Really! I promise!

Check out my newest song: Alive

Microcyb

 :P  Oh I see future scripts contests like that. LOL

eFishie

Contest idea: Buy a Cybiko, play my Paranoia demo, and recreate it in PHP. First to do so, you'll win a prize.
--Jon

Abstraction Point Electronic Music Community - We don't bite! Really! I promise!

Check out my newest song: Alive

Aquilo

this morning I did use the examples posted to make mine more efficient - I really did dislike using header(location) to show the image.

but "@readfile()" is slower then "file_get_contents()" so I went with it!
I played with the three variables at the top to make them one legal line since I made the path a string! ;D

<?
(($p = 'C:\\Inetpub\\wwwroot\\YaBBSE\\YaBBImages\\avatars\\') && ($d = opendir($p)) && ($i = -0));
while(false !== ($f = readdir($d)))
   (stristr($f, 'jpg') || stristr($f, 'gif') ?
      ($m[] = array($f, (stristr($f, 'jpg') ? 'jpg' : 'gif'))) && ($i++) : $i);
closedir($d);
Header('Content-Type: image/' . $m[($r=rand(0, $i-1))][1]);
echo file_get_contents($p.$m[$r][0]);
?>


6 lines of code.

would it be possible to add output compression in one line??

[Unknown]

<?php
ob_start
('ob_gzhandler') && ($p = 'C:\\Inetpub\\wwwroot\\YaBBSE\\YaBBImages\\avatars\\') && ($d = opendir($p));
while(
false !== ($f = readdir($d)))
 
stristr($f, 'jpg') || stristr($f, 'gif') ? ($m[] = array($f, (stristr($f, 'jpg') ? 'jpg' : 'gif'))) : 0;
closedir($d) && header('Content-Type: image/' . $m[($r = rand(0, count($m) - 1))][1]) && print file_get_contents($p.$m[$r][0]);
?>


Tecnically 4 lines of code.  In reality a lot more :P.

-[Unknown]

Aquilo

reality? that's where the pizza guy comes from right?

eFishie

My updated script rocks! And best yet: It's on Hotscripts! BEAT THAT! 8) :P
--Jon

Abstraction Point Electronic Music Community - We don't bite! Really! I promise!

Check out my newest song: Alive

SuPaFly

#16
hey guys,

How do I start all those codes.. I made a file name test.php ( put the codes in it and saved it on my server ) in my directory but I dont know how to link it?!

Im trying to have my sigs randomly change but I cant do it. I read all your post and I dont get it.. Could someone plz tell me the steps..

Tks for you help!
hxxp:www.edit-strike.nl/supafly.htm [nonactive]

original work by G.B

Chris Cromer

#17
Quote
My updated script rocks! And best yet: It's on Hotscripts! BEAT THAT! 8) :P
Mine takes in groups of avatars, and even lets you choose which avatars to display from those groups.

http://www.agigames.com/avatar/index.php (shows all avatars)

http://www.agigames.com/avatar/index.php?groups=gundam (shows gundam avatars)

http://www.agigames.com/avatar/index.php?groups=anime (shows anime avatars)

http://www.agigames.com/avatar/index.php?groups=misc,megaman (shows both misc avatars and megaman avatars)

http://www.agigames.com/avatar/index.php?groups=megaman;avatars=zero.gif,megaman.gif (shows the zero.gif or megaman.gif avatars from the megaman group of avatars)

The avatar script displays gif, png, and jpg images. And it automatically uses avatars in the directories which means all I have to do to add more avatars is just upload them.

I wonder if I should submit mine to hotscripts. ;)
Chris Cromer

"I was here, here I was, was I here, sure I was" - The little voice in my head.

SuPaFly

#18
just wanted to let people know that Chris Cromer is pretty nice, he helped me out!!   :D
hxxp:www.edit-strike.nl/supafly.htm [nonactive]

original work by G.B

videokiller

Hi, my friend asked me to make him a random avatar script and gave this forum as a referance to want he want. I'm ok with php, but I have a few questions :

1-Is the php script in a file (with the .php extention) and I put a link to that file in the signature box in the profile of my friend at the forum he want, and if so how do I link it exactly

2-If it's not a file, how do I put php code in the signature box

3-Is'nt the fact that anybody can have php code executed on the server a big security leak, I mean somebody's script could be to erase everything on the server.

thanks
Jim

Advertisement: