Forum Moderators: coopster

Message Too Old, No Replies

Random Images

         

gargoyle1

12:07 am on Jun 17, 2005 (gmt 0)

10+ Year Member



Ok so I've been playing with random image scripts, and I've gotten decent enough results.

I put the script into the directory with the images, it gives ma a random one. We have a pretty good relationship.

Recently, I've been wanting more from my random image scripts. I notice that if I put 2 random images on a page, they both give me the same one.

Is there any way to alter the script so that it returns a new image in each instance of the script that runs on the page?

Here's the one that I'm using right now:
<?
//read folder
$folder=opendir(".");

while ($file = readdir($folder))

$names[count($names)] = $file;

closedir($folder);
//sort file names in array
sort($names);
//remove any non-images from array
$tempvar=0;
for ($i=0;$names[$i];$i++){
$ext=strtolower(substr($names[$i],-4));
if ($ext==".jpg"¦¦$ext==".gif"¦¦$ext=="jpeg"¦¦$ext==".png"){$names1[$tempvar]=$names[$i];$tempvar++;}
}
//random
srand ((double) microtime() * 10000000);
$rand_keys = array_rand ($names1, 2);
//random image from array
$slika=$names1[$rand_keys[0]];

//image dimensions
$dimensions = GetImageSize($slika);

header ("Location: $slika");
?>

anshul

6:36 am on Jun 17, 2005 (gmt 0)

10+ Year Member



Here is the code, I've written to store unique numbers in an array:
$max=23;
$total_numbers=9;
$random = array();
for($i = 0; count($random) < $total_numbers; $i++) {
$rand = rand(1, $max);
if(!in_array($rand, $random)) {
$random[$i] = $rand;
}
}
$random = array_values($random);

gargoyle1

7:28 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



Thanks for your random number script. I dont' seem to have any problems actually generating the randomness of image, I have trouble with the browser recognizing the code as different images.

I think that part of the problem is that if I have this:

<img src="images/randomscript.php">

appear several times on a page, the browser stores the image, and simply retrieves the same image for every instance.

Is there a way to have it combat this?

RonPK

9:01 pm on Jun 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way is probably to add a random, unique string to the request:

img src="images/randomscript.php?18283yehd"

That way the browser will consider all instances of randomscript.php to be unique.

anshul

6:48 am on Jun 18, 2005 (gmt 0)

10+ Year Member



I meant select a random image based on random numbers. I recently did this ( successfully ) for managing affiliates ;)

gargoyle1

3:35 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Attach a meaningless random string to the end...

*slaps self on forehead*

I knew that!

Thanks for the help guys. ;)
I'll just go stand in my big dumb oaf corner now. LOL

anshul

6:06 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Oh! I again meant store all images in an array and choose an image based on a random number! May be I'm thinking wrong: whatever you do, good luck, gargoyle :)