Page 1 of 1

Image rotation on refresh.

Posted: Wed Jun 29, 2005 10:34 pm
by Jeefo
Are there any PHP scripts that will change an image when the page is refreshed (or the page is loaded again)? Nothing fancy, I just need a simple PHP code. Thanks for your help.

Posted: Wed Jun 29, 2005 10:36 pm
by John Cartwright
How are all your images stored?
In a database? Stored in a folder? -- or a combination of both.
Do you want the image to be randomly chosen, or in sequence?

Posted: Wed Jun 29, 2005 10:40 pm
by Jeefo
They'll be stored in a folder, but does it matter when the images and the script are in the same directory? Chosen randomly is fine.

Posted: Thu Jun 30, 2005 3:07 am
by Chris Corbyn
Something like:

Code: Select all

$dir_of_images = '/path/to/directory/'; //Keep trailing slash
$handle = opendir($dir_of_images);
$images = array();
while ($file = readdir($handle)) { //Loop over the files
    $ext = substr($file, -3);
    if ($ext == 'jpg' || $ext = 'gif') { //Add any other 3 letter extensions here
        $images[] = $file; //Store image in array
    }
}
shuffle($images); //Shake them all up 
echo '<img src="'.$dir_of_images.$images[0].'" alt="" />'; //Output the image

Posted: Thu Jun 30, 2005 11:06 am
by Jeefo
d11wtq wrote:Something like:
Thanks, but even though I specified the directory of the images, they don't show up at all. Also, I need the images to be clickable.