-3

I am trying to display a certain amount of random images from one directory on my website without displaying duplicates.

I found this question: Display 2 random pictures PHP with no duplicates which partially answers my problem:

<?php
$pics = array('image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg');
$images = array_rand($pics, 2);
?>

<img src="images/<?php echo $pics[$images[0]]; ?>" />
<img src="images/<?php echo $pics[$images[1]]; ?>" />

The problem I have is that the images are uploaded to the folder with completely different/random names such as 1a2265fg65444.jpg 55v4423097ww6.jpg etc, so I can't manually add them to the $pics array. I need to somehow scrape the directory for *.jpg and have the array generated automatically

I did try Michal Robinsons answer on Show Random images from a folder without repeating using JS or PHP but couldn't get it to print anything for some reason:

$all_images = glob("/images/photos/{*.jpg}", GLOB_BRACE);
shuffle($all_images); 
$images = array();
foreach ($all_images as $index => $image) {
     if ($index == 15) break;  // Only print 15 images
     $image_name = basename($image);
     echo "<img src='/images/photos/{$image_name}' />";
}

Perhaps I'm missing something?

Any pointers would be simply awesome.

Many thanks

  • Are you sure `/public/directory/{$image_name}` is the right path? – Arkadi Apr 01 '18 at 10:02
  • Hi Arkadi, thanks for the reply. I did use my own image path on my website, the code in my question is just taken from the referenced posts. – FredBentley Apr 01 '18 at 10:21
  • @FredBentley Can you provide more details regarding your setup? Where is you upload dir? Maybe an `ls` or a `dir` showing image names and paths? – urban Apr 01 '18 at 10:27
  • write `print_r($all_images); die();` before `foreach` and check what's printed – Arkadi Apr 01 '18 at 10:29
  • I updated the 2nd block of code above to show that my images are in /images/photos/ there are 70 images in there at present so for example https://www.mywebsite.com/images/photos/55v4423097ww6.jpg is one. I tried your suggestion Arkadi, but it seems to kill the script. Could it be something to do with $images = array(); not being referenced again in the code? Should $image later in the code also be $images? – FredBentley Apr 01 '18 at 10:38
  • Arkadi, I see what your line of code does. It doesn't show anything :/ – FredBentley Apr 01 '18 at 10:47
  • Fixed, I changed: glob("/images/photos/ to glob("images/photos/ – FredBentley Apr 01 '18 at 10:52
  • Thank you for your help guys :) – FredBentley Apr 01 '18 at 10:52

2 Answers2

3

try this

$scan = scandir("/images/photos/");
shuffle($scan);
$r = rand(2, count($scan)); // maybe (count($scan) - 1)
printf("<img src='/images/photos/%s' />", basename($scan[$r]));
0

Thanks Aron, but I managed to fix it by changing:

glob("/images/photos/ to 
glob("images/photos/

To complicate things further though, I just realized I need to show 1 image from a 1st folder followed by another image from a 2nd folder, and repeat this about 5 times, still without showing any duplicate :/ This is as far as I have got, but seem to be digging a bigger hole that doesn't work, as I'm stuck at the foreach...:

$all_images1 = glob("images/photos/{*.jpg}", GLOB_BRACE);
$all_images2 = glob("images/photos1/{*.jpg}", GLOB_BRACE);
shuffle($all_images1); 
shuffle($all_images2); 
$images1 = array();
$images2 = array();
$class = 1; 
foreach ($all_images1 as $index1 => $image1) {
if ($index1 == 10) break;  // Only print 15 images
$image_name1 = basename($image1);
echo "<img src='/images/photos/{$image_name1}' class='image".$class++."' /> <img src='/images/photos1/{$image_name2}' class='image".$class++."' />";
}

Am I going in the wrong direction to do this?

Thanks Ted