Echo a random image.
Moderator: General Moderators
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Echo a random image.
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!
I couldnt find anything on Google?
Can you help me?
Thanks!
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Easily peasily 
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 arrayhere'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))];
}My version:
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 
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)."' />";Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
Yes, it is flawed...it doesn't work...i like your thinking thopickle wrote:My version:
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 flawedCode: Select all
$image_dir = "/path/to/pics"; $files = array_diff(glob($image_dir),glob($image_dir,GLOB_ONLYDIR)); echo "<img src = '".array_rand($files)."' />";
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)]."' />";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)])."' />";image.php:
then link to image.php in img:
Alternatively, instead of linking to image.php..
Code: Select all
<?php
$files = glob('/path/to/images/*.{jpg,jpeg,gif,png}', GLOB_BRACE);
$rand = array_rand($files);
readfile($files[$rand]);
?>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}\" />";
Last edited by Jenk on Fri Jan 27, 2006 5:53 am, edited 2 times in total.
- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York