3

I have to generate random image from a directory. I know which is simple like,

   $dire="images/";
   $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
   $randomImage = $images[array_rand($images)];
   <input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />

But I have to make sure that each image from that directory picked at least one time before generating second time randomly. The above code only will display only any random image.

My thought is, I have to store the random image in an array and check the array every time with newly created random image. If the new random image is not in that array, I need to display that image,else I have to find another image.

I created the below code with above thought.

  $allimgs=array();
  $dire="images/";
  $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
  $randomImage = $images[array_rand($images)];

   if(!in_array($randomImage,$allimgs))
   {
     $allimgs[]=$randomImage;
     <input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />
   }

But I am still stuck with this code. Anyone please help to improve this code? or any other idea?

Thanks.

NewPHP
  • 608
  • 2
  • 15
  • 28
  • Do you need the image paths after you display them? You could remove them from your `$images` array each time you use an image. – Nate Aug 09 '12 at 02:49
  • Do you mean show all the images one time, or only show one image, and change to another after page refresh? – xdazz Aug 09 '12 at 02:52

4 Answers4

3

One solution might be to do this:

<?
// initialize $images
shuffle($images);
$randomImage = array_pop($images);
?>
<input type="image" src="<?=$randomImage;?>" alt="<?=$randomImage;?>" />

This will guarantee that you use each image only once, in a random order.

Gordon Bailey
  • 3,881
  • 20
  • 28
2

One alternative to array_rand is shuffle:

$dire="images/";
$images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
shuffle($images);

And then to display the next random image:

$randomImage=array_pop($images);

When the array is empty you call the initialization code again. So putting it together:

$images=array()  //Initialize once at top of script
$dire="images/";
...
if(count($images)==0){
  $images = glob($dire. '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
  shuffle($images);
  }
$randomImage=array_pop($images);

(I deliberately repeated the glob() so that any new images get discovered on the second iteration.)

P.S. I'm assuming you understand that, by default, PHP is stateless. If this is supposed to give each user a different image each time they visit a page (e.g. a rotating banner ad), then the code is almost the same, but move the $images array into $_SESSION.

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • @user3466544 Thanks for the suggested edit. (It got rejected because you put comments in the source code explaining what you had changed: next time just add the semi-colons, and use the edit comment field to explain anything.) – Darren Cook Apr 09 '14 at 23:52
0

These codes helps to pull random images from folder

       <?php 
            $files = glob('photos/*'); 
               natcasesort($files);      // Display images  
            foreach($files as $file) {     
               echo '<img src="' . $file . '"/>';
            }  
        ?>
Keshav
  • 1
0

Here's my answer, which seems to work OK. It produces one image generated randomly from the images in a given folder:

<?php 
$result = glob('images/*'); 
shuffle($result);
foreach($result as $result) {     
   break;     
}  
?>
<img src="<?php echo "$result"; ?>" />
Chris9
  • 157
  • 1
  • 6