derelektrischemoench

Resize getRandom() image

Recommended Posts

Hi guys,

I'm trying to resize images which I query through getRandom(). I can't seem to get it right though. What I have so far is:

 

$randomImage = $pages->get($targetPageId)->images->getRandom()

Which returns an image, albeit in original resolution. I assumed I should be able to call resize() on the image like so:

$randomImage = $pages->get($targetPageId)->images->getRandom()->width(400)

Which unfortunately gives me an exception. I assume this is because getRandom() might not return an image but something else(?).

So how would I do this correctly?

 

Greetings

derelektrischemoench

 

Share this post


Link to post
Share on other sites

Look / debug what you get from ->images->getRandom(), maybe via die(var_dump($randomImage));

Then you will know what to do, or you come back with the new information and ask how to proceed further.

Share this post


Link to post
Share on other sites

The function getRandom() returns null if the field is empty and not an empty object. You cannot call a function from null. You need to check first if the image object exists. Make also sure your selector $targetPageId includes only pages having a field 'images'. Try this:

$randomImage = !empty($pages->get($targetPageId)->images->getRandom())? $pages->get($targetPageId)->images->getRandom()->width(400) : null;

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By rolisx
      Hi all
      I have a problem here. I created a gallery with 240 pictures. Created an images field with no maximum amount (0). Unfortunately, only 98 of the 240 images show on the website. Any idea what I possibly could have done wrong? Thanks for your help!
      <?php foreach ($page->images as $image) { $options = array( 'quality' => 90, 'upscaling' => false ); $thumb = $image->size(250, 250, $options); ?> <div class="col-xl-2 col-lg-3 col-md-4 col-sm-6 col-6 foto"> <a href="<?= $image->url ?>" data-lightbox="lightbox" > <img src="<?= $thumb->url ?>" alt="" > </a> </div> <?php } ?>  
    • By Marco Angeli
      Hi there,
      I migrated my site from dev to live server. Copied all files in the new location, transferred MySQL db from dev to live.
      The site works properly, but the images not.
      Even the processwire logo in control panel is not visible: is it something related with files permissions?
      Is there an easy way to solve this issue?
      EDIT: if I update a page via control panel, adding a new image, the thumbnail is blank...
    • By louisstephens
      Ill be honest, I am a bit unsure how accomplish this. I have a repeater (dev_repeater) that contains an image field set to 1 image. Nested within this repeater, is another repeater (dev_child_repeater) that allows a user to add in some urls. However, there is also a hidden field that I am trying to pass the parent repeater's image path. 
      I know I can output all the data by using:
      <?php foreach($page->dev_repeater as $repeater) { foreach($repeater as $url) { # do some stuff } } ?> For the life of me, I can not figure out how to obtain the image url in my php to pass to a variable inside the nested foreach loop. Hopefully this made some sense.
    • By Xonox
      Hi,
      I have a template that it's working fine in development, however I can't get it to work on production! It shows every information inside repeater fields except the images.
      Here's the template:

      These are the circuit_day_image settings:

      This is the code:
      <?php foreach($page->circuit_days as $circuit_day) { if($circuit_day->circuit_day_image) { $day_image = $circuit_day->circuit_day_image->size(300, 300)->url; echo '<img src="' . $day_image . '" />'; } else { echo 'No image! :('; } echo '<h2>' . $circuit_day->title . '</h2>'; echo $circuit_day->body; } ?> I always get "No image! :("
      I think I'm doing everything right!
      Anyone else with a similar problem?
      Update
      After uploading the production database into my server, the images stopped working. It can be one of two problems:
      1. Bad field configuration;
      2. Something wrong with the Database.
      I can't find the problem. Any suggestion is welcome, thanks,
      Update 2
      I forgot to upload the images. It's working on dev and not on production. Still no clue!
      Clue 1
      When I insert
       <pre><?php print_r($circuit_day); ?></pre>
      On development I get a clean list for each repetition:

      However, on production, the command gets on a weird recursive loop that takes forever (it even slows the browser to a halt):

      What might be going on?
    • By LMD
      I'm wondering if it is possible to add a label to the description input in image fields (in the admin).
      I'm using the module Image Extra, which has labels for each input, but I'd like to add a label to the default 'description' input too. The image below illustrates this:

       
      If there is no way (no hook?) then, I suppose I could just not use the default description and add a new description input with the Image Extra module.  But I thought I'd ask in case I (or others) ever want to do this without using the module (i.e. just the one input required).
      I'm using PW 3.0.98
      Thanks.
      ---
      FYI: yes, that is a cat and not a quilt -- this is on my local dev server and I don't have the actual photos yet! She is on a quilt, so it counts... technically.