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 1
Link to comment
Share on other sites

5 minutes ago, bernhard said:

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.

That's it! Thanks. I owe you a beer 🍻

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