Random Avatar

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

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

Random Avatar

Post by Nay »

I got the script from Nigma a few weeks back. I kept on re-writing it to suit me. I've come up with this:

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");

$dir = "/home/chaosdre/public_html/external/signature/images";
$dh = opendir($dir);
$images;

while (($file = readdir($dh)) !== false) {
$images .= $file . "-";
}

$images = substr($images, 5);
$images = explode("-", $images);

$max = count($images);
$i = rand(0, $max);
floor($i);

$image = "images/" . $images[$i];

readfile($image);

closedir($dh);

?>
I'm wondering why I need to use:

Code: Select all

$images = substr($images, 5);
I put the images into a string, then explode() to make them an array. But before I do that, I need to use substr() to get rid of some dots in front of the string. Before making them into an array, the string is:
.-..-myimage1.jpg-myimage2.jpg
Those aren't the real image names, lol. Anyhow, it's .-.. since I added the - before I made them into a string. So basicly, what I'm asking is where are these two dots coming from? I have NO files in the images/ directory other than the images themselves.

Thanks,

-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

. and .. are directory references
. represents the current directory
.. the parent directory (if there is any)

since you only want files to be taken into consideration you might simplify your script with

Code: Select all

<?php
$dir = '/home/chaosdre/public_html/external/signature/images/';
$images = array();

$dh = opendir($dir);
while (($file = readdir($dh)) !== false)
{
	if (is_file($dir.$file))
		$images[] = $file;
}

$selected = array_rand($images, 1);

// debug output
echo $images[$selected]; 
?>
php.net/array_rand wrote:As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

ow, always a simpler version to my scripts eih Volka? hehe

Thanks,

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

Post by Nay »

Hey, is there anyway that I can make "more chances" to a certain image?

-Nay
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

i searched over for array_rand and there's no need to use srand to seed the random function, but u have to use if if you don't have PHP 4.2.0+
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well, yah...

If you use a counter combined with % modulus, or some other math function, you might be able to automaticly insert the favourite image into your array more than once on different places...
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post by pootergeist »

new builds of php (4.3.0 and above) could also support using glob() to build the array

Code: Select all

header(.....);
chdir('/home/chaosdre/public_html/external/signature/images/');
readfile(array_rand(glob('*.jpg')));
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Adding something that might be to much, but... ;)

Code: Select all

CREATE TABLE `imagecounter` (
  `number` int(11) unsigned NOT NULL auto_increment,
  PRIMARY KEY  (`number`)
) TYPE=MyISAM;

Code: Select all

// and above any file/image calculation, we add:
    require('db.php'); // or whatever
    mysql_query("insert into imagecounter values('')");
Change the above to fit your needs. Perhaps a "update counter set counter = counter +1" would be much better, but I just tested it fast without really much thought. Add referrer, time, whatever...

You then have a "X many people have seen my avatar"-script.

Of course, having the avatar displayed on 15 sites, numerous uncountable pages and being vewed more than frequently would increase the stats dramaticly (heavy traffic warning), but i like to explore ideas. ;)
Post Reply