Jump to content

Multiple random and unique images on a page


formmailer
 Share

Recommended Posts

Hi!

I use the following to display a random image on a page:

$image = $page->images->findTag('inline-body')->getRandom();

 

This works of course really fine, but on some pages I want to use 2 or more random images. The images have to be on different places and the same image shouldn't be displayed twice.

I assume that I have to something like:

$a = $page->images->findTag('inline-body');
$image = $a->getRandom();
echo "<img src='$image->url'>";
$a->remove($image);

and with later in the template, for the next image, I'll just do:

$image = $a->getRandom();
echo "<img src='$image->url'>";
$a->remove($image);

Would this make sense? Or is there a better way?

 

//Jasper

Link to comment
Share on other sites

1 hour ago, abdus said:

Try


// get two random images
$a = $page->images->findTag('inline-body')->shuffle()->slice(0, 2);

https://processwire.com/api/ref/wire-array/

Thanks Abdus!

Using:

$a = $page->images->findTag('inline-body')->getRandom(2);

Would be even easier.
Problem is that the template doesn't specify location of the images defined. These are inserted in the body text field using Hanna Code. That makes the above a bit harder to apply.

My own idea was to create a code snippet in Hanna that could be re-used as many times as needed. If I can check if the array $a exists and only create a new one if it doesn't, it should be possible to do so. 
I know that it is not the fastest code, but since all the pages are being cached by ProCache I am not worried about this process taking an extra second or two. 

/Jasper

  • Like 1
Link to comment
Share on other sites

10 hours ago, formmailer said:

inserted in the body text field using Hanna Code. That makes the above a bit harder to apply.

10 hours ago, formmailer said:

a code snippet in Hanna that could be re-used as many times as needed

Not tested, but you can store any information inside $page (or any class that extends WireData), and do something like this:

<?php // Hanna Code template

if (!$page->randomImages) {
    $page->randomImages = $page->images->findTag('inline-body')->shuffle();
    $page->randomIndex = $page->randomImages->count;
}

if (--$page->randomIndex >= 0) {
    // as per @Robin S's suggestion:
    // $img = $page->randomImages[$page->randomIndex];
    $img = $page->randomImages->eq($page->randomIndex);
    echo "<img src='$img->url'/>";
}

Keep in mind, this will stop showing images after all images have been used.

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Thanks again.

Unfortunately my plan is not going to work, because the Hanna Code generates the same result if the Hanna tag is added multiple times. I validated this by making a Hanna code snippet with the following:

echo(mt_rand(10,100));

I added the Hanna tag 3 times and the result was 3 times the same integer. 

 

//Jasper

Link to comment
Share on other sites

The solution from @abdus works well - you just need to change...

$img = $page->randomImages[$page->randomIndex];

...to...

$img = $page->randomImages->eq($page->randomIndex);

...because Pageimages are indexed by basename not integer.

2 hours ago, formmailer said:

Hanna Code generates the same result if the Hanna tag is added multiple times

This must be the result of some efficiency optimising in Hanna Code. To get around this, just add some attribute to each tag - you don't need to use this attribute anywhere in your tag code. So lets say the attribute is called "seed" (just so it looks kinda legit to the user - this attribute is not actually used to seed anything):

[[random_image seed="546458"]]
...some content...
[[random_image seed="afdasfd"]]

So this attribute's value can be anything so long as it's not the same as another tag on the same page.

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

One more (final) question. Is there an easy way to make this work for pages with only one image? That would allow me to use the same hanna tag on every page, not only on the ones that have more than one image.

//Jasper

Link to comment
Share on other sites

9 minutes ago, formmailer said:

pages with only one image?

I'm guessing for image fields with single image, where $page->images returns Pageimage instead of Pageimages?

Then you can use getUnformatted() method:

<?php // Hanna Code template

if (!$page->randomImages) {
    $images = $page->getUnformatted('images'); // always returns PageImages array
    if ($images->count > 1) $images = $images->findTag('inline-body')->shuffle();
    $page->randomImages = $images;
    $page->randomIndex = $images->count;
}

if (--$page->randomIndex >= 0) {
    // as per @Robin S's suggestion:
    // $img = $page->randomImages[$page->randomIndex];
    $img = $page->randomImages->eq($page->randomIndex);
    echo "<img src='$img->url'/>";
}

 

  • Thanks 1
Link to comment
Share on other sites

Thanks again!

For some reason this is not working.

27 minutes ago, abdus said:

if (--$page->randomIndex >= 0) {

But this is:

$page->randomIndex;

if (--$page->randomIndex >= -1) {

I am not sure why this is working but it is. :)

Link to comment
Share on other sites

It should've worked. $images->count is 1 when there's a single image, so --$page->randomIndex >= 0 returns true.

But when there are multiple images but none matches 'inline-body' tag, then $images->count is 0 and --$page->randomIndex >= 0 returns false.

image.png.07a593a82c1acb3156bdef6c86085a93.png

 

Edit:

14 minutes ago, formmailer said:

if (--$page->randomIndex >= -1) {

If you use >= -1, the last image may be shown twice. Because when randomIndex reaches 0 (i.e. all images have been shown),  it will return true with --$page->randomIndex >= -1 at the next hanna tag.

  

image.png.7a6fdb3fca7508416e10780aba157ce3.png

  • Like 1
Link to comment
Share on other sites

For some reason the count() always return one image less than the actual number of pictures.

If the page has 3 images with the correct tag, the count is 2. Strange but it seems to be that way....

 

 

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