Page 1 of 1

Echo a random image.

Posted: Wed Jan 25, 2006 9:38 pm
by nickman013
I have a folder of pictures, I want the page to post one of the pictures at random, and each time a person visits the site, it will be random.

I couldnt find anything on Google?

Can you help me?

Thanks!

Posted: Thu Jan 26, 2006 8:45 am
by Chris Corbyn
Easily peasily :P

Code: Select all

$folder = '../some/folder'; //No trailing slash

$images = array();

$handle = opendir($folder);
while ($file = readdir($handle))
{
    $ext = substr($file, -3);
    if ($ext == 'jpg' || $ext == 'gif' || $ext == 'png')
    {
        $images[] = $file; //Add the file to the array
    }
}
closedir($handle);

shuffle($images); //Jumbly jumbly, shake 'em all about!

echo '<img src="'.$folder.'/'.$images[0].'" alt="" />'; //Print the first one in our shuffled array

Posted: Thu Jan 26, 2006 8:50 am
by JayBird
here's my version which i did the other day actually:

Code: Select all

function randomImage() {

	$directory = "/var/www/ub/images/random/";
	
    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {

        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);
	
	echo '/images/random/'.$results[rand(0, (count($results)-1))];
	
}

Posted: Thu Jan 26, 2006 12:36 pm
by pickle
My version:

Code: Select all

$image_dir = "/path/to/pics";
$files = array_diff(glob($image_dir),glob($image_dir,GLOB_ONLYDIR));
echo "<img src = '".array_rand($files)."' />";
I just wrote this up on the spot and haven't tested it so I now eagerly await someone to point out how this method is fundamentally flawed ;)

Posted: Thu Jan 26, 2006 2:52 pm
by nickman013
First of all thank you all for responding with your scripts. I tried the first one that was posted, d11wtq's. And it worked!

Thank You All!

Posted: Fri Jan 27, 2006 5:15 am
by JayBird
pickle wrote:My version:

Code: Select all

$image_dir = "/path/to/pics";
$files = array_diff(glob($image_dir),glob($image_dir,GLOB_ONLYDIR));
echo "<img src = '".array_rand($files)."' />";
I just wrote this up on the spot and haven't tested it so I now eagerly await someone to point out how this method is fundamentally flawed ;)
Yes, it is flawed...it doesn't work...i like your thinking tho :wink:

path to pics needs *.* on the end.

array_rand($files) returns the KEY not the VALUE.

You would need to do this - $files[array_rand($files)]

There are two way to make the script work...one liek this using a realtive path to the images

Code: Select all

$image_dir = "images/*.*"; 

$files = array_diff(glob($image_dir),glob($image_dir,GLOB_ONLYDIR)); 

echo "<img src = '".$files[array_rand($files)]."' />";
or like this using the full server path

Code: Select all

$image_dir = $_SERVER['DOCUMENT_ROOT']."/images/*.*"; 

$files = array_diff(glob($image_dir),glob($image_dir,GLOB_ONLYDIR)); 

echo "<img src = '".str_replace($_SERVER['DOCUMENT_ROOT'], "", $files[array_rand($files)])."' />";

Posted: Fri Jan 27, 2006 5:43 am
by Jenk
image.php:

Code: Select all

<?php

$files = glob('/path/to/images/*.{jpg,jpeg,gif,png}', GLOB_BRACE);

$rand = array_rand($files);

readfile($files[$rand]);

?>
then link to image.php in img:

Code: Select all

<img src="http://www.yourdomain/image.php" />

Alternatively, instead of linking to image.php..

Code: Select all

$imagepath = str_replace($_SERVER['DOCUMENT_ROOT'], 'http://www.yourdomain.com', $files[$rand]);

echo "<img src=\"{$imagepath}\" />";

Posted: Fri Jan 27, 2006 5:52 am
by JayBird
hehehe, is this turning into a one-up-manship thread :lol:

Posted: Fri Jan 27, 2006 5:54 am
by Jenk
The first one I posted I use for random/rotatin forum signatures :P

Posted: Fri Jan 27, 2006 10:19 am
by nickman013
Thank you all for replying.

I got what I needed. Thanks alot!