Page 1 of 2

Random Images - since we have avatars

Posted: Thu Apr 18, 2002 3:31 pm
by Craig
Since we can have avatars on this board I thought folks might be interested in this little snippet that I learned from Zef Hemel over at IBforums.

It enables you to have a random avatar everytime the page loads here.

Take the following code and save it as something.jpg ;)

Code: Select all

<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");
srand((double)microtime()*1000000);
$nr=rand(1,20);
header("Location: $nr.gif");
echo "blah";
?>
change the 20 to however many images you want
rename all your chosen images to 1.gif 2.gif 3.gif etc

create a .htaccess file and write this in it

Code: Select all

AddType application/x-httpd-php .jpg
save the file- upload everything to the one directory and call your jpg - it should give you a random image each time. :)

Neat

Posted: Thu Apr 18, 2002 3:33 pm
by jason
Oh...this should be fun....hehehe, I have some good ones I can use

Posted: Thu Apr 18, 2002 6:00 pm
by heschong
A simpler way is something like this:

(call this file whatever.php)

Code: Select all

<?php
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
Header("Pragma: no-cache");
Header("Content-Type: image/jpeg");
srand((double)microtime()*1000000);
$nr=rand(1,20);
readfile($nr);
?>
Then just call that file as whatever.php/random.jpg (the last file name doesn't matter). You don't have to use any .htaccess files, and the browser doesn't have to redirect. Anything after the filname ("/random.jpg" in this case) is treated as $PATH_INFO data by Apache.

hey...

Posted: Thu Apr 18, 2002 8:15 pm
by g-force2k2
hey thats pretty neat stuff there 8) great job ;p i like how it works on your avatar Craig :D

do you know how i could get this code to work on my vbulletin? im just a newbie at php but i'm here to learn :D

Posted: Fri Apr 19, 2002 3:24 am
by Craig
It's easy ;) Just follow either of the ways above and link to your new image through your profile in vbulletin.

Posted: Fri Apr 19, 2002 5:42 pm
by sam
I had to edit those both together to get one that worked for me. You guys rock.

Cheers Sam
evilwalrus.com

Posted: Sat Apr 20, 2002 3:49 pm
by Craig
share your code sam, there's a good lad ;)
sam wrote:I had to edit those both together to get one that worked for me. You guys rock.

Cheers Sam
evilwalrus.com

Posted: Sat Apr 20, 2002 4:32 pm
by sam
Well I used the php directory functions to get the list of images, this way all I have to do is upload the images with any name and the script will use them.

Code: Select all

<?php 
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); 
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); 
Header("Pragma: no-cache"); 
Header("Content-Type: image/gif"); 
srand((double)microtime()*1000000); 

$i = 0;
$dir = opendir("icons");
while(($im = readdir($dir))) &#123;
    if($im != ".." && $im != ".")&#123;
       $image&#1111;$i] = $im;
       $i++;
    &#125;
&#125;  
closedir($dir);

$len = count($image)-1;
$n = rand(0,$len);

if(!readfile("icons/".$image&#1111;$n]))&#123;
   readfile("icons/error.gif");
&#125;

?>
Cheers Sam

Posted: Sat Apr 20, 2002 10:54 pm
by jason
I guess for the purposes of being complete, this is what I use:

Code: Select all

<?php 
Header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); 
Header("Expires: Thu, 19 Nov 1981 08:52:00 GMT"); 
Header("Pragma: no-cache"); 
Header("Content-Type: image/jpeg"); 
srand((double)microtime()*1000000); 

$images = array();
$name 	= '';
$fh 	= 0;
$nr		= 0;

$fh = opendir('.');
while ( $name = readdir($fh) )
&#123;
	if ( eregi("(gif|jpg|png|jpeg)$", $name) )
	&#123;
		$images&#1111;] = $name;
	&#125;
&#125;

$nr=rand(0,count($images)-1); 
readfile($images&#1111;$nr]); 

?>

Posted: Sat Apr 20, 2002 11:41 pm
by EvilWalrus
i've never seen such a tireless effort go into randomization of avatars :P

Posted: Thu Jun 06, 2002 6:18 pm
by Kriek
Hey Jason, where do you get all your avatars?
Some avatart site? I've always made my stuff from scratch, but I think would be fun to do some random images for a month or something. =)

Posted: Thu Jun 06, 2002 6:36 pm
by jason
Someone else had this up on their site in a directory, and was basically using it for the same thing. So I grabbed them up...resized them...and used them here. I honestly forget though, where I got them.

Posted: Thu Jun 06, 2002 7:08 pm
by Kriek
Hey that's cool man. Thanks.
I think I'm going to make 3-4 of Audrey Hepburn.
Of course they will be 1953 - 1967.

Posted: Fri Jun 07, 2002 12:42 pm
by Mithrandir
Hehe, don't you guys like my avatar? Just load again, and you'll like it :)

I'll post the code in a few moments, but i gotta find it first ;)


<edit>
Sorry, I can't find the code anymore :'(... But you might get an idea of how I did it :)

Posted: Fri Jun 07, 2002 3:09 pm
by Kriek
Interesting avatars Mith :wink: