Php pull random image from folder

I am wondering about the β€œbest” way to get a random image from a folder.

How, say, so that php simply selects a random image from a folder instead of searching and creating its array.

that's how i do it today

<?php $extensions = array('jpg','jpeg'); $images_folder_path = ROOT.'/web/files/Header/'; $images = array(); srand((float) microtime() * 10000000); if ($handle = opendir($images_folder_path)) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $ext = strtolower(substr(strrchr($file, "."), 1)); if(in_array($ext, $extensions)){ $images[] = $file; } } } closedir($handle); } if(!empty($images)){ $header_image = $images[array_rand($images)]; } else { $header_image = ''; } ?> 
+6
source share
4 answers

Try the following:

 <?php $dir = "images/"; $images = scandir($dir); $i = rand(2, sizeof($images)-1); ?> <img src="images/<?php echo $images[$i]; ?>" alt="" /> 
+11
source

Below code checks the list of images by image extension.

<?php function validImages($image) { $extensions = array('jpg','jpeg','png','gif'); if(in_array(array_pop(explode(".", $image)), $extensions)) { return $image; } } $images_folder_path = ROOT.'/web/files/Header/'; $relative_path = SITE_URL.'/web/files/Header/'; $images = array_filter(array_map("validImages", scandir($images_folder_path))); $rand_keys = array_rand($images,1); ?> <?php if(isset($images[$rand_keys])): ?> <img src="<?php echo $relative_path.$images[$rand_keys]; ?>" alt="" /> <?php endif; ?> 
+1
source
 function get_rand_img($dir) { $arr = array(); $list = scandir($dir); foreach ($list as $file) { if (!isset($img)) { $img = ''; } if (is_file($dir . '/' . $file)) { $ext = end(explode('.', $file)); if ($ext == 'gif' || $ext == 'jpeg' || $ext == 'jpg' || $ext == 'png' || $ext == 'GIF' || $ext == 'JPEG' || $ext == 'JPG' || $ext == 'PNG') { array_push($arr, $file); $img = $file; } } } if ($img != '') { $img = array_rand($arr); $img = $arr[$img]; } $img = str_replace("'", "\'", $img); $img = str_replace(" ", "%20", $img); return $img; } echo get_rand_img('images'); 

replace "images" with your folder.

+1
source

For several hours I searched the Internet to implement the code I wanted. I have compiled several different answers that I found on the Internet. Here is the code:

 <?php $folder = opendir("Images/Gallery Images/"); $i = 1; while (false != ($file = readdir($folder))) { if ($file != "." && $file != "..") { $images[$i] = $file; $i++; } } //This is the important part... for ($i = 1; $i <= 5; $i++) { //Starting at 1, count up to 5 images (change to suit) $random_img = rand(1, count($images) - 1); if (!empty($images[$random_img])) { //without this I was sometimes getting empty values echo '<img src="Images/Gallery Images/' . $images[$random_img] . '" alt="Photo ' . pathinfo($images[$random_img], PATHINFO_FILENAME) . '" />'; echo '<script>console.log("' . $images[$random_img] . '")</script>'; //Just to help me debug unset($images[$random_img]); //unset each image in array so we don't have double images } } ?> 

Using this method, I was able to implement opendir without errors (since glob() did not work for me), I was able to pull out 5 images for the carousel gallery and get rid of duplicate images and figure out empty values. One drawback of using my method is that the number of shots varies from 3 to 5 images in the gallery, probably due to the removal of empty values. Which did not bother me too much, as it works as needed. If someone can improve my method, I welcome you to do this. Working example (first carousel gallery on top of website): Eastfield Refinery

0
source

Source: https://habr.com/ru/post/913804/


All Articles