Simple Machines Community Forum

General Community => Scripting Help => Topic started by: Meriadoc on August 03, 2003, 01:32:29 AM

Title: Random Avatar Script
Post by: Meriadoc on August 03, 2003, 01:32:29 AM
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
Title: Re:Random Avatar Script
Post by: Overseer on August 03, 2003, 09:07:31 AM
Well i'm going to tweak this in a little bit to rotate banners :)

..maybe more too ;)
Title: Re:Random Avatar Script
Post by: Aquilo on August 03, 2003, 04:06:32 PM
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)]);
Title: Re:Random Avatar Script
Post by: Meriadoc on August 03, 2003, 05:22:26 PM
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
Title: Re:Random Avatar Script
Post by: Aquilo on August 04, 2003, 03:14:32 AM
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
Title: Re:Random Avatar Script
Post by: Meriadoc on August 05, 2003, 09:32:21 PM
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...
Title: Re:Random Avatar Script
Post by: Mecha Dude on August 17, 2003, 11:45:31 AM
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;
     }
}
?>
Title: Re:Random Avatar Script
Post by: eFishie on August 17, 2003, 12:21:03 PM
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.
Title: Re:Random Avatar Script
Post by: [Unknown] on August 17, 2003, 06:39:02 PM
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]
Title: Re:Random Avatar Script
Post by: eFishie on August 17, 2003, 11:21:08 PM
Damn you! >:(
Title: Re:Random Avatar Script
Post by: Microcyb on August 18, 2003, 05:14:47 PM
 :P  Oh I see future scripts contests like that. LOL
Title: Re:Random Avatar Script
Post by: eFishie on August 18, 2003, 10:55:01 PM
Contest idea: Buy a Cybiko, play my Paranoia demo, and recreate it in PHP. First to do so, you'll win a prize.
Title: Re:Random Avatar Script
Post by: Aquilo on August 19, 2003, 12:23:04 AM
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??
Title: Re:Random Avatar Script
Post by: [Unknown] on August 19, 2003, 12:40:48 AM
<?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]
Title: Re:Random Avatar Script
Post by: Aquilo on August 19, 2003, 12:45:14 AM
reality? that's where the pizza guy comes from right?
Title: Re:Random Avatar Script
Post by: eFishie on August 20, 2003, 07:07:34 PM
My updated script rocks! And best yet: It's on Hotscripts! BEAT THAT! 8) :P
Title: Re:Random Avatar Script
Post by: SuPaFly on October 12, 2003, 08:32:02 PM
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!
Title: Re:Random Avatar Script
Post by: Chris Cromer on October 12, 2003, 09:30:06 PM
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 (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 (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. ;)
Title: Re:Random Avatar Script
Post by: SuPaFly on October 12, 2003, 11:07:00 PM
just wanted to let people know that Chris Cromer is pretty nice, he helped me out!!   :D
Title: Re:Random Avatar Script
Post by: videokiller on October 14, 2003, 03:41:17 AM
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
Title: Re:Random Avatar Script
Post by: Tyris on October 14, 2003, 04:13:02 AM
well, the php script itself is hosted on your OWN server, so it can only do damage to that one, and not places the image is viewed at.

I'm pretty sure its a .php file that you just display with [img*]file.php[/img*] (without * of course)
to my knowledge, the browser tries to do display the 'image' located there, the server then runs the php script and creates the php output.
for a random avatar all you need to do is have the script randomly select an avatar and then display that.
Title: Re: Random Avatar Script
Post by: Cerberus on June 20, 2004, 07:52:20 AM
I'm using [Unknown]'s code
<?php
ob_start
('ob_gzhandler') && ($p = '/web/htdocs/site29/home/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]);
?>

the script is placed in /avatars but it isn't working: I get a 500 error. Any thoughts?
Title: Re: Random Avatar Script
Post by: BigMike on June 28, 2004, 04:39:56 AM
Try replacing:

ob_start('ob_gzhandler') && ($p = '/web/htdocs/site29/home/avatars') && ($d = opendir($p));

With:

ob_start('ob_gzhandler') && ($d = opendir("."));
Title: Re: Random Avatar Script
Post by: Cerberus on July 02, 2004, 07:20:36 AM
It isn't working, but I've found another script, which works well on my server
<?php
/*
-------------------------------------------------------------
|MD Random Image Generator                                  |
|Version 1.0.0                                              |
|This program is Copyright (c) Matthew Dingley 2003         |
|For more help or assistance go to MD Web at:               |
|www.matthewdingley.co.uk                                   |
|For information on how to install or for basic licence     |
|information, view below                                    |
|                                                           |
|This program is not to be used on a commercial site without|
|a commercial licence. Go to www.matthewdingley.co.uk for   |
|more information.                                          |
|                                                           |
|To install, just enter in the directory name that you store|
|the images in to the variable below named $dir. Upload any |
|images to that folder to have them randomly displayed.     |
|                                                           |
|You can also edit the variable $pattern if you know what   |
|you are doing.                                             |
|                                                           |
|To display the random image on your web page, you can      |
|either copy and paste all of this code into the page where |
|you want it or you can include it by putting in the        |
|following code into the page where you want the image:     |
|<?php include "image.php"; ?>
                              |
-------------------------------------------------------------
*/
$dir=opendir("/home/you/public_html/folder/");
//This is the directory route to the folder
$directory="";
//This is a relative link to the directory if it is not in the same directory as the file you are displaying the images on

$pattern="\.(gif|jpg|jpeg|png|bmp|swf)$";
if(!$dir)
{
die("Failed to read directory");
}
$s=readdir($dir);
$count="0";
$image;
while($s)
{
if(ereg($pattern, $s))
{
$image[$count]=$s;
$count++;
}
$s=readdir($dir);
}
closedir($dir);

//Spit it out
$limit=count($image);
$limit--;
$randNum=rand(0,$limit);
$size=getimagesize("$directory$image[$randNum]");
echo "<br><img src=\"$directory$image[$randNum]\" $size[3]>";
?>

:)
Title: Re: Random Avatar Script
Post by: Overseer on July 02, 2004, 08:47:23 AM
damn this thread old!


\/ well ya can see my randomise sig banner works! \/


need more entries though..
Title: Re: Random Avatar Script
Post by: evirvana on July 02, 2004, 10:03:32 PM
Okay, I'm new to all this, and I have never made PHP script in my life so can someone please explain in an easy way all the steps to making a random sig image? I have all the pics I need, and they are all uploaded, I just need to know how to make the script. Thanks for the help.
Title: Re: Random Avatar Script
Post by: Vinoth on July 08, 2004, 01:49:49 PM
this would consume more BW in total,

btter to have an single avatar to display
Title: Re: Random Avatar Script
Post by: Chris Cromer on July 08, 2004, 04:09:22 PM
Well as long as you don't use extremely huge images then it will be fine. :P

I have over 400 something avatars in rotation on plenty of big sites including sites like gamespot.com, this site, my own sites, etc. And my site only gets around 1GB of bandwidth usuage a month, and that includes all the downloads of utilities, games, images, movies, etc. So my point is, it doesn't use that much bandwidth. :P
Title: Re: Random Avatar Script
Post by: The Mole on July 08, 2004, 07:30:40 PM
Quote from: evirvana on July 02, 2004, 10:03:32 PM
Okay, I'm new to all this, and I have never made PHP script in my life so can someone please explain in an easy way all the steps to making a random sig image? I have all the pics I need, and they are all uploaded, I just need to know how to make the script. Thanks for the help.

I'll help you out so long as you have your own website, with php, already.

-the mole
Title: Re:Random Avatar Script
Post by: Kerux on March 21, 2005, 04:21:00 PM
Quote from: <? Piranha($fx); ?> on August 20, 2003, 07:07:34 PM
My updated script rocks! And best yet: It's on Hotscripts! BEAT THAT! 8) :P

but unfortunately it can no longer be downloaded
Title: Re: Random Avatar Script
Post by: Owdy on November 02, 2009, 01:47:21 PM
Does anyone have good script what supports png jpg and gif ?

yes i know this topic is old :P
Title: Re: Random Avatar Script
Post by: DavidCT on November 02, 2009, 02:23:11 PM
It looks like Cerberus's code (Reply #23) does, no?


$pattern="\.(gif|jpg|jpeg|png|bmp|swf)$";


Title: Re: Random Avatar Script
Post by: Blah blah on November 02, 2009, 03:28:35 PM
You could just use this site

http://signavatar.com