Jump to content

Display Random Images from Folder


Omzy

Recommended Posts

I need help writing some code to display 7 random images from a folder. I have managed to make it display one random image but I just cannot figure out how to make it display multiple random images from a set of 20.

 

The images in the folder will always be JPEG and are named 'image_x.jpg' (x = a number from 1-20)

 

Optionally it could auto-count the number of images in the folder to dynamically assign the end value, but that isn't too important.

Link to comment
Share on other sites

I need help writing some code to display 7 random images from a folder. I have managed to make it display one random image but I just cannot figure out how to make it display multiple random images from a set of 20.

 

The images in the folder will always be JPEG and are named 'image_x.jpg' (x = a number from 1-20)

 

Optionally it could auto-count the number of images in the folder to dynamically assign the end value, but that isn't too important.

 

I'd recommend glob and array_rand

 

EDIT: You can use count on the array of files to get the auto-count, but it's pointless to append numbers to a filename, like that, not as fast.

Link to comment
Share on other sites

OK I tried using array_rand() however I realised I need to output ALT text on the images. So I created an associative array ('$x'=>'alt text') and then ran a foreach loop over the array_rand() array. But I don't think array_rand() works with associatiave arrays.

 

Is there a better way of doing this?

Link to comment
Share on other sites

OK I tried using array_rand() however I realised I need to output ALT text on the images. So I created an associative array ('$x'=>'alt text') and then ran a foreach loop over the array_rand() array. But I don't think array_rand() works with associatiave arrays.

 

Is there a better way of doing this?

 

If you were to provide the code you have it would be easier for us to see what you were already doing and provide an appropriate solution. Here is what I would do:

 

//Array to hold jpeg names and associative text
$imageList = array(
    'image_1.JPG' => 'Text for image 1.',
    'image_2.JPG' => 'Text for image 2.',
    'image_3.JPG' => 'Text for image 3.',
    'image_4.JPG' => 'Text for image 4.',
    'image_5.JPG' => 'Text for image 5.'
/// Etc.
);

//Create array of just the images
$images = array_keys($imageList);
//Randomize the image array
shuffle($images);
//Get 7 random images
$randomImages = array_slice($images, 0, 7);
//Display the images and the text
foreach($randomImages as $image)
{
    echo "<img src=\"folderpath/{$image}\"><br />\n";
    echo "{$imageList[$image]}<br />\n";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.