Jump to content

Grabbing a random image


Pete
 Share

Recommended Posts

Hi folks

What I'd like to do is grab a random image to display. It's not as simple as grabbing a random image from a specific page though - I've created a Page Headers page with each child page being a specific category which contains multiple images for that category (Natures, Movies etc.).

Therefore what I need to do is pick a random image based on a template - in this case it's called page-headers.

This is pseudo-code, but how I'd imagine it working is something like $pages->find("template=page-headers")->images->getRandom();

Is it possible to do it that way or would I have to grab every page, iterate through them building a list of all images and pick one from random from that list?

Link to comment
Share on other sites

  • 8 months later...

I've used similar code and got it displaying 6 random images, however I want it to show 6 random images from across all the pages that share the gallery_entry template. At present it's only showing from the same album (page):

<?php $images = $pages->get("template=gallery_entry, sort=random")->images->getRandom(6);

foreach ($images as $image) { 
$img = $image->size(80,60);
?>

<a href="<?php echo $config->urls->root; ?>albums"><img src="<?php echo $img->url; ?>" width="<?php echo $img->width; ?>" height="<?php echo $img->height; ?>" alt="Latest Photos" /></a>

<?php }

?> 

Any idea on how to get random images from more than one page? Thanks

Link to comment
Share on other sites

I would suggest grabbing one randomly from each page:

$entries = $pages->find("template=gallery_entry, sort=random, images.count>0, limit=6"); 
$images = array();

foreach($entries as $entry) {
   $images[] = $entry->images->getRandom(); 
}

foreach($images as $image) {
   $img = $image->size(80,60);
   echo "<a href='/albums/'><img src='{$img->url}' alt='Latest photos' /></a>";
}
  • Like 2
Link to comment
Share on other sites

You can use a WireArray and fill it with image objects from pages. Then get a random count from that WireArray. It's also in the cheatsheet.

// new WireArray object for storing images
$images = new WireArray();

// we find all pages using a specific template that have at least 1 or more images.
// assumes the image field is named "myimages". If we deal with really lots of pages/images, consider using a limit to avoid performance problems
$pa = $pages->find("myimages.count>0, template=basic-page, sort=random, limit=10");

// loop all found pages and import the images to the $images object
foreach($pa as $p){
 $images->import($p->myimages);
}

// output 4 random images from the wire array
foreach($images->getRandom(4) as $img){
 echo "<img src='{$img->url}'/><br/>";
}

Thanks Ryan, is it possible to get all the images that use the template and then randomly choose 6 photos?

Reading again, and looking at Ryans code, it does pretty much the same, just little different way.

Think about it again...

Is there really any difference ?

From: get ALL images from ALL pages and choose 6 random from them.

To: get randomly 6 pages from ALL pages and choose 1 randomly from each page.

I don't see really a difference, only that the second is far better and the way to go, because you limit the find for pages in the first place. Getting all images from all pages could create problems and not so scalable.

  • Like 3
Link to comment
Share on other sites

Soma, it's completely different... if you have 2 pages, one with 100 images and the other with only one, with that method we would get the sole image of the second page 50% of the times... I think what onjegolders wants, it's to have all pictures to be in equal circumstances.

Link to comment
Share on other sites

Soma, it's completely different... if you have 2 pages, one with 100 images and the other with only one, with that method we would get the sole image of the second page 50% of the times... I think what onjegolders wants, it's to have all pictures to be in equal circumstances.

Yep that's exactly right Diogo, before I was getting pictures always from the same "album" (page) and that page changed randomly.

Your code Soma looks like it could work so thanks guys!

Link to comment
Share on other sites

Yep that's exactly right Diogo, before I was getting pictures always from the same "album" (page) and that page changed randomly.

Your code Soma looks like it could work so thanks guys!

Looking at your code, yes. Because ->get() will always get only 1 page and 6 random images from that page. Ryan's example won't do that because it uses ->find() and returns 6 random pages everytime.

As I said it's not exactly the same, of course it depends a lot how many pages and images you have. But going with a case of 20+ pages with ~20 images each will work as good. Considering having 10000+ pages with each ~20 images, Ryans example would work still as good, but grabing all pages and images will most likely end in a out of memory.

Link to comment
Share on other sites

  • 1 year later...
 

Being that I was sorting through pages and then sorting through a repeater and then sorting through an image field array and then only wanted one and really didn't care which one it was as long as it was one of them and it was possibly different each time or at least as often as no one noticed.

I was so glad to find that there is such a thing as getRandom() just now!

Happiness can be easily won.

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...