Jump to content

Getting images in a module


heldercervantes
 Share

Recommended Posts

Hey guys. Another noob question about modules.

I'm making a module that, when a page is saved, generates a json file that will be read from an outside app to get the latest news. I'm almost there, got the module working and generating the json file nicely, but I'm still struggling with images.

Basically I want to output something like `$page->image->size(300,300)->url`, but that doesn't work saying size isn't available in this context. So I tried passing it without the size, but then the url only outputs the image's assets directory, not the file itself. Couldn't find anything in the docs ?

Link to comment
Share on other sites

I guess because you are in a module you have output formatting OFF which means that $page->image is an array of images and not a single PageImage object. Using $page->image->first()->size should work.

PS: To make everything clear and reduce chances for errors you can be more verbose and use $page->getUnformatted('image')->first()...

PPS: Better check if an image was uploaded before using ->size(...)

$img = $page->getUnformatted('image');
if($img AND $img->count()) {
  $img = $img->first();
  $img->size(...)->...
}

 

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

  • 1 year later...
14 hours ago, Jonathan Lahijani said:

I'm going to staple this to my head!

Please don't 🙂 We have a new way of doing that, because ->getUnformatted() also gets temporary items, which is likely not what you want.

This is how to do it in recent versions: $page->get("images.first") --> I've searched hard but did not find the blog post about it 😞 Anyone?

  • Like 1
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...