Random Images - since we have avatars

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

Random Images - since we have avatars

Post 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. :)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Neat

Post by jason »

Oh...this should be fun....hehehe, I have some good ones I can use
heschong
Forum Newbie
Posts: 3
Joined: Thu Apr 18, 2002 5:56 pm
Location: Johnson City, TN

Post 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.
g-force2k2
Forum Newbie
Posts: 12
Joined: Thu Apr 18, 2002 8:15 pm

hey...

Post 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
Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

Post by Craig »

It's easy ;) Just follow either of the ways above and link to your new image through your profile in vbulletin.
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post by sam »

I had to edit those both together to get one that worked for me. You guys rock.

Cheers Sam
evilwalrus.com
Craig
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 3:13 pm

Post 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
User avatar
sam
Forum Contributor
Posts: 217
Joined: Thu Apr 18, 2002 11:11 pm
Location: Northern California
Contact:

Post 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
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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]); 

?>
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

i've never seen such a tireless effort go into randomization of avatars :P
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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. =)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post 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.
Mithrandir
Forum Newbie
Posts: 1
Joined: Fri Jun 07, 2002 12:42 pm
Contact:

Post 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 :)
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Interesting avatars Mith :wink:
Post Reply