Jump to content

Grab random images + the pageurl


Philipp
 Share

Recommended Posts

I have multiple pages with the template "licht" and these pages contain images(in a field called "portfolio"). My idea is to have an overview about all those images an their corresponding pages.

I couldn't figure out the code to select (for example) 24 random images and their pageurl.

My code so far:

<?php
$i = 0;
//Made a wireArray for the 24pages containing the images
$bilder = new WireArray();
// Find all target pages
$seiten = $pages->find('template=licht ,portfolio.count> 0');
//Fill the array with the pages.
// I might not have 24 unique pages so I use them multiple times.
while($i < 24) {
$bilder->add($seiten->getRandom());
$i++;
}
// Loop with the Imagepages
foreach($bilder as $bild):
?>
//Output
<a href="<?=$bild->url;?>">
<img src="<?=$bild->portfolio->getRandom()->size(180,180)->url;?>" height="150" width="150"/>
</a>

<?php endforeach;?>

One problem - as explained in the source code- is that I might only have less than 24 unique pages. So I want to grab more than one image from every page.

I only could get 24 random images but then I can get the right URL..

tl:dr: I want to grab 24 random images from multiple Pages and I want to link to the corresponding page, and thats why I need the url

Link to comment
Share on other sites

You can't add multiple instances of the same page to a WireArray like you want. PW makes them unique.

If you really want to "fill" the gap if not 24 pages are found I would use something like the following code example I just tested and works fine. Maybe there's other ways of doing it but you get the idea.

$limit = 10;
$bilder = new PageArray();
$seiten = $pages->find("template=basic-page, images.count>0, limit=$limit, sort=random");
$found = $seiten->count();
foreach($seiten as $bild){
   echo <<<OUT
   <a href="{$bild->url}">
       <img src="{$bild->images->getRandom()->size(180,180)->url}" height="150" width="150"/>
   </a>
OUT;
}
if($found < $limit){
   while($found <= $limit-1){
       $rp = $seiten->getRandom();
       echo <<<OUT
       <a href="{$rp->url}">
           <img src="{$rp->images->getRandom()->size(180,180)->url}" height="150" width="150"/>
       </a>
OUT;
   $found++;
   }
}
Link to comment
Share on other sites

A more cleaned up code example: :)


function renderItem($pa) {
   $out = "<a href='{$pa->url}'>";
   $out .= "<img src='{$pa->images->getRandom()->size(180,180)->url}' height='150' width='150'/>";
   $out .= "</a>";
   return $out;
}

$limit = 10;
$bilder = new PageArray();
$seiten = $pages->find("template=basic-page, images.count>0, limit=$limit, sort=random");
$found = $seiten->count();

foreach($seiten as $bild){
   echo renderItem($bild);
}
if($found < $limit){
   while($found < $limit){
       $rp = $seiten->getRandom();
       echo renderItem($rp);
       $found++;
   }
}

Still I wouldn't do this, because it could be that it grabs the same image from the same page in a row.

So I would try to limit the image to a count you're sure there's at least. SO if you don't have 24 page with images there's no need to output 24 items.

  • Like 1
Link to comment
Share on other sites

Thanks, It works and the images are linked to the right page.

Still I wouldn't do this, because it could be that it grabs the same image from the same page in a row.

I want to grab X Images from each page, but together no more than 24.

For Example:

Page A has 28 Images

Page B has 10 Images

Page C has 7 Images

So I've a total of 45 Images and 3 pages. Now I want to get a random image of this "imagepool" until I have 24. I think it was your code I've found on the forum that I'm using right now:


 $images = new WireArray();
 $pa = $pages->find("portfolio.count>0, template=licht, sort=random");

 foreach($pa as $p){
   $images->import($p->portfolio);
 }

 foreach($images->getRandom(27) as $img):

I'll try to get this together ;)

Link to comment
Share on other sites

Easy enough:

function renderItem($img) {
 $out = "<a href='{$img->page->url}'>";
 $out .= "<img src='{$img->size(180,180)->url}' height='150' width='150'/>";
 $out .= "</a>";
 return $out;
}

$limit = 10;
$bilder = new WireArray();
$seiten = $pages->find("template=basic-page, images.count>0, sort=random");

foreach($seiten as $seite){
 $bilder->import($seite->images);
}

foreach($bilder->slice(0,$limit) as $b) {
 echo renderItem($b);
}

Again, your thinking is not recommended of getting images randomly until you have 24 images, because you'll end up having multiple of the same image. And it gets too complicated to bare with it if you don't have 24 images.

I would grab just random set of limited pages and images and output them.

Link to comment
Share on other sites

  • 2 years later...

I am resurrecting this cool thread...

I am reading this with great interest. What I would like to do is have a specific folder on a site server that contains around 20-30 images. Then on the site Home Page have a div that displays a single random photo from this folder: a fresh photo each time the page is loaded. 

And the kicker to this: I would also like to present a "would you like to see another random photo?" with a reload button or some CSS style link.

Any thoughts on this?

Thanks!

Link to comment
Share on other sites

Is the use of a dedicated folder important? Add an image field to the homepage and then output a random image from those images in the newly created field

When the field name with mutliple images is random_photos

<? echo $page->random_photos->getRandom()->url;?>

This will return a random photo from the images field every time you reload the page.

EDIT: To make it prettier:

<?php $random_image = $page->random_photos->getRandom();?>
<img src="<?php echo $random_image->url;?>" alt="<?php echo $random_image->description;?>"/>
  • Like 2
Link to comment
Share on other sites

That works great Philipp!

This effect is done with just two lines of code using the PW API. Very cool! No special folders, no plugins, no modules, no scripts, no fuss. This would also world nicely for a random banner image...

Edited by PhotoWebMax
Link to comment
Share on other sites

  • 3 weeks later...

Hi,

I'm using @soma's code below fine. How would I do a check so that I can only output images under a certain width or height? My attempt in bold.

<?php
function renderItem($pa) {
    $out = "<li>";
    $out .= "{$pa->title}<a href='{$pa->url}'><img src='".$pa->portfolio_images->first()->getCrop('thumbnail', "quality=65")->url . "' alt=''></a>\n";
    $out .= "</li>";
    return $out;
}

$limit = 32;
$artist_images = new PageArray();
$artists = $pages->find("template=portfolio, portfolio_images.count>0, limit=$limit, sort=random");
$found = $artists->count();

foreach($artists as $artist_images){
	if($artist_images->portfolio_images->width < '1200' XOR $artist_images->portfolio_images->height < '1200') {
	    echo renderItem($artist_images);
	}
}
if($found < $limit){
    while($found < $limit){
        $rp = $artists->getRandom();
        echo renderItem($rp);
        $found++;
    }
}

A more cleaned up code example: smile.png
 

function renderItem($pa) {
    $out = "<a href='{$pa->url}'>";
    $out .= "<img src='{$pa->images->getRandom()->size(180,180)->url}' height='150' width='150'/>";
    $out .= "</a>";
    return $out;
}

$limit = 10;
$bilder = new PageArray();
$seiten = $pages->find("template=basic-page, images.count>0, limit=$limit, sort=random");
$found = $seiten->count();

foreach($seiten as $bild){
    echo renderItem($bild);
}
if($found < $limit){
    while($found < $limit){
        $rp = $seiten->getRandom();
        echo renderItem($rp);
        $found++;
    }
}
Link to comment
Share on other sites

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...