Jump to content

Displaying a Random Image from any web page


mcgarriers
 Share

Recommended Posts

Hi,

New to ProcessWire but already thoroughly enjoying developing with it.

I have a Home Page and I'd like to show a random image from ANY of my web pages on it.

The only thing is it needs to find the image from a child page of the parent with ID '1010'.

Can someone explain how I achieve this?

Thank you,

Michael

Link to comment
Share on other sites

Hi michael,

Something like this should work (assuming your image field is called 'images'):

$img = $pages->get('parent=1010, images.count>0, sort=random')->images->getRandom();
echo "<img src='{$img->url}' alt='{$img->description}'>";
  • Like 2
Link to comment
Share on other sites

Hi Michael.

Welcome to PW!

The following should work. Doing this in a hurry. There are other ways to do this...this is just one of them :)

// grab and output a random image from the child page with ID 123
$image = $pages->get(123)->images->getRandom();
if($image) echo "<img src='$image->url'>";

See this page for more details on images...

OK, am late in my reply :)

Edit: Plus just seen you want images from any child pages; apologies. Will leave this response here though

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Welcome to the forum mcgarriers.

You can do it like this:

$parent = $pages->get(1001);

// here I'm using the images array of the first child of 1001 to create  a new empty array of the same kind (look on the cheatsheet for this method)
$a = $parent->child->images->makeNew();

// iterate through all the pages and import their images to the created array
foreach ($parent->children as $c) {
    $a->import($c->images);
}

// our array holds all the images that we want, Now you can get a random image
$randomImage = $a->getRandom();
 

Edit: I've just seen Wanze's answer. The difference is that he is getting a random page, and then a random image. Like this, an image that is alone in a page has more chances of being shown than images in more crowded pages. While with my method they all have the same chances.

  • Like 3
Link to comment
Share on other sites

Many thanks for your replies, peeps :-)

Wanze, I get this error message:

Error: Call to a member function getRandom() on a non-object (line 43 of *************\home.php)



@diogo - many thanks for the reply.

Using your code, I get the following error:

Error: Call to a member function makeNew() on a non-object

Link to comment
Share on other sites

Are your sure that the field is named 'images'?

Also if you have set 'Maximum files allowed' to 1, then the code needs little modification:

// With max files set to '1'
$img = $pages->get('parent=1010, images.count>0, sort=random')->images;
// Check if we have an image 
if ($img->id) {
  echo "<img src='{$img->url}' alt='{$img->description}'>";
} else { 
  echo "No page with image found..."; 
}
  • Like 1
Link to comment
Share on other sites

Welcome to the forum mcgarriers.

You can do it like this:

$parent = $pages->get(1001);

// here I'm using the images array of the first child of 1001 to create  a new empty array of the same kind (look on the cheatsheet for this method)
$a = $parent->child->images->makeNew();

// iterate through all the pages and import their images to the created array
foreach ($parent->children as $c) {
    $a->import($c->images);
}

// our array holds all the images that we want, Now you can get a random image
$randomImage = $a->getRandom();
 

Edit: I've just seen Wanze's answer. The difference is that he is getting a random page, and then a random image. Like this, an image that is alone in a page has more chances of being shown than images in more crowded pages. While with my method they all have the same chances.

I thought it would be the opposite...

However this solution is slower than Wanzes.

Link to comment
Share on other sites

Hi @diogo,

Do you know if it's possible I can retrieve the Page ID associated with the Random Image that is selected?

like this: $randomImage->page (edit: gets the page itself, not the id, for the id use $randomImage->page->id)

http://processwire.com/api/cheatsheet/#files

However this solution is slower than Wanzes.

Yep, it's really a mather of choosing what is most important in this case. If there will be an proximate number of images in each page, going with Wanze's method makes sense. If there will be pages with 1 image and others with 20, my method guarantees that the choice will be really random at the cost of being slower.

  • Like 1
Link to comment
Share on other sites

Here a little helper module that does it optimized using direct SQL. https://gist.github.com/somatonic/5568201

If you install module you can do:

$helper = $modules->get("RandomImages"); // load module somewhere before

// getRandomImages(count, field, parent)
$image = $helper->getRandomImages(1,"images", 1001);
echo "<img src='{$image->size(150,0)->url}'>";

Or to get more than 1

$helper = $modules->get("RandomImages"); // load module somewhere before

// getRandomImages(count, field, parent)
$images = $helper->getRandomImages(2,"images", 1001);
foreach ($images as $key => $image) {
    echo "<img src='{$image->size(150,0)->url}'>";
}
  • Like 5
Link to comment
Share on other sites

Maybe it would make sense to add a find() and get() method to $files. Think  of how easy it would be to achieve a task like this:

$randomImage = $files->find("page.parent=1010")->getRandom();

edit: there isn't any reference to files in the db. I guess it wouldn't be easy to do this...

Link to comment
Share on other sites

  • 5 months later...

Getting an error

Error: Call to a member function getRandomImages() on a non-object

on line

$image = $helper->getRandomImages(1, "images", 1847);

where '1847' is the parent of pages with an 'images' field.

And the module doesn't show up in the list of modules although it's in the modules folder.

Link to comment
Share on other sites

// grab and output a random image from the child page with ID 123
$image = $pages->get(123)->images->getRandom();
if($image) echo "<img src='$image->url'>";

This works excellent for something I'm doing. Just wondering if you can also grab the url of the page where the random image resides. To make the image clickable and go to the page, not the image itself.

Link to comment
Share on other sites

Doesn't that code assume that you know already the page?

To you it probably does.... I'll give it some trial and error or perhaps a night sleep and see if it does to me as well.

Thank you for the encouragement ;)

<a href='{$img->page->url}'><span class='newspapersubtitle'>{$img->description}</span></a>
Edited by BFD Calendar
Link to comment
Share on other sites

  • 2 years later...
On 13/05/2013 at 11:37 AM, Wanze said:

$img = $pages->get('parent=1010, images.count>0, sort=random')->images->getRandom();

Hope it is OK to follow on from an old thread? This form of solution works great for me... but if I want also want to identify the image by tag how I would I do that?

$sub_image = $pages->get("has_parent.id=$sub->id,template=product, sort=random")->product_images->getTag('prod')->getRandom();

does not work... bit flummoxed.

Reason for wanting to do this is that there are different types of images in this field only some that are appropriate.

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