How to Get Random Image from Directory Using PHP

How to get random image from directory using PHP

$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments

You can send a 2nd argument to array_rand() to get more than 1.

Pulling Random Image From Directory with PHP

AH! Thanks for pointing me in the right direction.

I moved the 404 images folder into the same directory as my theme and everything is working now

the updated code looks like

<h1>404 Page not Found</h1>
<?php
$imageDir = '404/';
$images = glob($imageDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)];

?>
<img src="<?php echo($randomImage); ?>" alt="" />

php generate random image from a directory

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.

Display Random Image from WP Directory

You just need to replace this line:

$file_link = $folder_path . "/" . $random_file;

...with this one:

 $file_link =  get_site_url(null, $folder_path . "/" . $random_file);

What was the problem?

scandir is returning the physical file path, not the url to the file - we can confirm this is we look at one of the values it returns for $random_file, e.g. wp-content/uploads/H-PH-MA-Greyscale

This is a relative URL, but we need an absolute URL so that it will work no matter where it is called. We also need to include the site URL so that the path to wp-content is correct. (Note: site URL and home URL can be different - the site URL is the one that always point to the location of the WP files).

Another problem is the trailing slash (or lack of one) on the site URL. get_site_url and get_home_url do not add the trailing slash to the URL, so you need to add it yourself if it isn't included in the file path.

So how do we solve this?

Using the get_site_url function added the correct path to the WP files, and it also lets us pass it the file path with our without a preceding slash as a parameter, and it will add the slash on the path only if it is needed.

PHP display random n images from directory

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);



Related Topics



Leave a reply



Submit