Echo a random image.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Echo a random image.

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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))];
	
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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 ;)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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)])."' />";
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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}\" />";
Last edited by Jenk on Fri Jan 27, 2006 5:53 am, edited 2 times in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hehehe, is this turning into a one-up-manship thread :lol:
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

The first one I posted I use for random/rotatin forum signatures :P
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Thank you all for replying.

I got what I needed. Thanks alot!
Post Reply