Page 1 of 1

Random Avatar

Posted: Sat Oct 04, 2003 2:57 am
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

Posted: Sat Oct 04, 2003 3:15 am
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.

Posted: Sat Oct 04, 2003 3:18 am
by Nay
ow, always a simpler version to my scripts eih Volka? hehe

Thanks,

-Nay

Posted: Sat Oct 04, 2003 3:46 am
by Nay
Hey, is there anyway that I can make "more chances" to a certain image?

-Nay

Posted: Sat Oct 04, 2003 7:07 am
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+

Posted: Sat Oct 04, 2003 7:15 am
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...

Posted: Sat Oct 04, 2003 8:50 am
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')));

Posted: Mon Oct 13, 2003 8:36 pm
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. ;)