How to get 15 random images from a folder in WordPress?

I need to get 15 random images from a folder and show them on a page:
I tried the following code, however it did not do what I wanted:

$string =array();
$filePath='wp-content/themes/tema/img-test/';  
$dir = opendir($filePath);
while ($file = readdir($dir)) { 
   if (eregi(".png",$file) || eregi(".jpg",$file) || eregi(".gif",$file) ) { 
   $string[] = $file;
   }
}
while (sizeof($string) != 0){
  $img = array_pop($string);
  echo "<img src='$filePath$img'  width='100px'/>";
}

Related posts

5 comments

  1. So, you have all the files in $string array, that’s good.

    You can either use the rand() function to get some random integer in the arrays size:

    $string = ['img1.jpg','img2.jpg','img3.jpg'];
    $rand = rand(0,count($string)-1);
    echo $string[$rand];
    

    You would have to loop that.

    Or, you could use array_rand() which will automate all that:

    $string = ['img1.jpg','img2.jpg','img3.jpg'];
    $amount = 3;
    $rand_arr = array_rand($string, $amount);
    for($i=0;$i<$amount;$i++) {
        echo $string[$rand_arr[$i]] ."<br>";
    }
    
  2. You could do this using the glob() function native to PHP. It will get all files in a directory. Following that you can pick one file from the retrieved list.

    $randomFiles = array();
    $files = glob($dir . '/*.*');
    $file = array_rand($files);
    for ($i = 0; $i <= 15; $i++) {
        $randomFiles[] = $files[$file];
    }
    
  3. Use this code. Your random image will be available in $arRandomFiles.

    $filePath = 'wp-content/themes/tema/img-test/';  
    $files = glob($filePath. '*.{jpeg,gif,png}', GLOB_BRACE);
    $arKeys = array_rand($files, 15);
    $arRandomFiles = array();
    foreach ($arKeys as $key) {
        $arRandomFiles[] = $files[$key];
    }
    var_dump($arRandomFiles);
    
  4. Simple function that handles that

    <?php 
    function getImg( $path ) {
        $filePath= $path . '*';
        $imgs = glob( $filePath );
    
        if( $imgs ) {
            $i = 1;
            foreach( $imgs as $img ) {
                if( $i <= 15 ) {
                    $ext = pathinfo( $img, PATHINFO_EXTENSION );
                    if( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) 
                        $r[] = $img;
                }
                else 
                    break;
                $i++;
            }
            shuffle( $r );
            return $r;
        }
        else 
            return array();
    }
    
    print_r( getImg( 'wp-content/themes/tema/img-test/' ) );
    
  5. You can try function like:

    function getRandomFile($directory)
    {
        $directoryIterator = new DirectoryIterator($directory);
        $count = iterator_count($directoryIterator) - 2;
        foreach ($directoryIterator as $fileInfo) {
            $last = $fileInfo->getRealPath();
            if ($fileInfo->isFile() && (rand() % $count == 0)) {
               break;
            }
        }
    
        return $last;
    }
    

Comments are closed.