Jump to content


Photo

Grabbing a random image


  • Please log in to reply
10 replies to this topic

#1 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 30 July 2011 - 07:14 AM

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?

#2 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3118

  • LocationAtlanta, GA

Posted 30 July 2011 - 07:32 AM

I'm responding from phone so keeping it short:

$image = $pages->get("template=page-headers, sort=random")->images->getRandom();


#3 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 30 July 2011 - 08:58 AM

Awesome stuff ryan - works a treat :)

#4 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 25 April 2012 - 09:11 AM

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

#5 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3118

  • LocationAtlanta, GA

Posted 25 April 2012 - 11:35 AM

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>";
}


#6 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 25 April 2012 - 12:46 PM

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

#7 diogo

diogo

    Hero Member

  • Moderators
  • 1,997 posts
  • 1074

  • LocationPorto, Portugal

Posted 26 April 2012 - 01:32 PM

Have a look here http://processwire.c..._+merge +arrays
I think you will find your answer there (sorry for the short answer, I'm in a hurry)

#8 Soma

Soma

    Hero Member

  • Moderators
  • 3,191 posts
  • 1745

  • LocationSH, Switzerland

Posted 26 April 2012 - 03:51 PM

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.

@somartist | modules created | support me, flattr my work flattr.com


#9 diogo

diogo

    Hero Member

  • Moderators
  • 1,997 posts
  • 1074

  • LocationPorto, Portugal

Posted 27 April 2012 - 04:36 AM

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.

#10 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 27 April 2012 - 06:50 AM

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!

#11 Soma

Soma

    Hero Member

  • Moderators
  • 3,191 posts
  • 1745

  • LocationSH, Switzerland

Posted 27 April 2012 - 07:12 AM

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.

@somartist | modules created | support me, flattr my work flattr.com





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users