1

I want to display random n number of images from a folder. Currently i am using this script to display images

<?php
$dir = './images/gallery/';
foreach(glob($dir.'*.jpg') as $file) { ?>
<div class="item"><img src="<?php=$file;?>"></div>
<?php } ?>

I want only 10 (or n number) images, that too randomly. How to do this?

user3045457
  • 164
  • 1
  • 3
  • 12

3 Answers3

1

The shuffle() method will put the elements of a given array in a random order:

<?php
$dir = './images/gallery/';

function displayImgs($dir, $n=10){
$files = glob($dir.'*.jpg');
shuffle($files);
$files = array_slice($files, 0, $n);
foreach($files as $file) { ?>
<div class="item"><img src="<?php=$file;?>"></div>
<?php } 
} ?>

Usage: displayImgs("/dir/temp/path", 20);

Jitendra Kumar. Balla
  • 1,173
  • 1
  • 9
  • 15
  • I am not sure if this is going to display random images – Manikiran Oct 21 '15 at 05:16
  • n is default equal to 10 in this function. If you have 10 images in an array then you will address them as 0 through 9 or (10-1) here – PC3TJ Oct 21 '15 at 05:22
  • I know that @TravisConnelly , just ensuring Jitendra posts a well-rounded answer, not leaving much speculation from the OP ;-) – Darren Oct 21 '15 at 05:25
  • @JitendraKumar.Balla I am getting error `Warning: array_slice() expects parameter 1 to be array, boolean given in...` – user3045457 Oct 21 '15 at 05:37
  • @user3045457, Updated code, shuffle() will return boolean. so that is the small mistake. – Jitendra Kumar. Balla Oct 21 '15 at 05:47
  • @JitendraKumar.Balla your script working fine. But here is a issue., I have 30 images in folder. If i set $n to 10, it shows 20 images. Here the $n subtracts $n from total images and shows remaning images – user3045457 Oct 21 '15 at 05:57
  • @user3045457, Yes your right, I am passing $n so from that it will get all the element's from $n to array length, So if your passing one more arg, then it will works fine, array_slice($files, 0, $n); And its very simple to use , Updated code. – Jitendra Kumar. Balla Oct 21 '15 at 06:43
1

Well, this might be overkill, but you can also use a directory iterator and some randomness to achieve this. I used a modified version of the random numbers generation function from this answer.

make sure that the path you give to the function is relative to the directory in which the script resides, with a slash at the beginning. The __DIR__ constants will not change would you happen to call this script from different places in your file hierarchy.

<?php

function randomImages($path,$n) {

    $dir = new DirectoryIterator(__DIR__. $path);

    // we need to know how many images we can range on
    // but we do not want the two special files . and ..
    $count = iterator_count($dir) - 2;

    // slightly modified function to create an array containing n random position
    // within our range
    $positionsArray = UniqueRandomNumbersWithinRange(0,$count-1,$n);

    $i = 0;
    foreach ($dir as $file) {

        // those super files seldom make good images
        if ($file->getFilename() === '.' || $file->getFilename() === '..') continue;

        if (isset($positionsArray[$i])) echo '<div class="item"><img src="'.$file->getPathname().'"></div>';

        $i++;
        // change the count after the check of the filename,
        // because otherwise you might overflow
    }
}

function UniqueRandomNumbersWithinRange($min, $max, $quantity) {
    $numbers = range($min, $max);
    shuffle($numbers);
    return array_flip(array_slice($numbers, 0, $quantity));
}
Community
  • 1
  • 1
0

Let us first create a array and push some random numbers into it. And as per you let $n be 10.

$n = 10;
$arr = array();
for($i = 1; $i <= $n; $i++){
    /* Where $n is the limit */
    $rand = rand($n);
    array_push($arr, $rand);
}

So now we have an array containing the random digits and now we have to echo out the images by iterating over the array:

foreach($arr as $image){
    $intToStr = (string) $image;
    foreach(glob($dir. $intToStr . '.jpg') as $file){
        echo "<div class='item'>$file</div>";
    }
}

This would echo out your images.

Ikari
  • 3,176
  • 3
  • 29
  • 34