Random Avatar

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Random Avatar

Post by Nay »

Yes, yes, it's about time someone dropped it into this forum and save the MySQL space for PM's (to me :lol:). Anyhow, I was going nuts on why IPB didn't let me add my avatar so I went and revised it a little. Though in the end you'll have to add as:
// works in IPB:

http://thedomain.com/thepath/index.php/dummyimage.jpg

// works in PHPBB:

http://thedomain.com/thepath/?picture.jpg

// or just still: (yes, this is also only for phpbb)

http://thedomain.com/thepath/
Anyhow, here's my revised version:

Code: Select all

<?php

// chanage the directory path
$dir = 'C:/AppServ/www/tests/rand/';

// set valid extensions
$extentions = array('jpg', 'gif', 'png');

$images = array();

$dh = opendir($dir);

   while(($file = readdir($dh)) !== false) {

      // check if it's a file, and it has a valid extension
      if (is_file($dir . $file) && in_array(substr($file, -3), $extentions)) {

         // add image to array
         $images[] = $file;

      }

   }

closedir($dh);

// get 1 random key
$selected = array_rand($images, 1);

// set headers
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");

// read image
readfile($images[$selected]);

?>
-Nay
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

The version I'm using for my random avatar uses the same path method (http://mysite.com/avatar/) but is a bit smaller code wise. It does however require you to add the image names manually and doesn't detect them automatically.

Code: Select all

<?php ob_start();

$img = array("angry","bored","happy","love","sad","smile");
shuffle($img);
reset($img);

header("Content-type: image/gif");
readfile($img[0].".gif");

ob_end_flush(); ?>
Seems to work ok (as you can see) ;)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

And, as always, you can find mine here:

http://www.devnetwork.net/forums/images ... mages.phps

And I just stick it in the same directory, here:

http://www.devnetwork.net/forums/images/faces/

And call it like so:

http://www.devnetwork.net/forums/images ... /image.jpg
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

I'm glad Jason don't like blondes a lot.
Post Reply