Jump to content

$image->height() results in Error (non-object Call)


Ole
 Share

Recommended Posts

First of all: I'm PHP-trainee. :)

I hope, I didn't overlooked the answer in a thread, but I couldn't find anything solving during my search.
(And don't judge me for my naming.) ;)

The Problem:

I build my Output in a function, because I need the same layout for different cases. Everything works fine, but the $image->height method.

generaterowitem(wire("pages")->get($resultid));

[...]

function generaterowitem($finalarrayitem) {
 $firstimage = $finalarrayitem->job_image->first();
 <img src="'.$firstimage->url.'" alt="'.$firstimage->description.'" />


I think, the first line is quite simple. $resultid contains a PageID and I get a single Page. This part is in a other function. Therefore the (wire("pages")...

The Layout is build in the generaterowitem function. The Script works fine and the <img> Tag loads the image in original size quite well. But if I add the ->height method,

 $firstimage = $finalarrayitem->job_image->first()->height(145);

I get:

Fatal error: Call to a member function height() on a non-object


Shouldn't ->height also works, if the standard ->url did fine?

Link to comment
Share on other sites

Hello there, and welcome to the forum!

To answer your last question: yes, it should work. This seems like a strange one, I can't seem to spot the issue right away.

It kind of sounds like there was a case where job_image->first() returned nothing (or, at least, something other than an object). Is there a single call to that function in this page and you're sure that the target page has an image? You're not by any chance iterating over multiple pages and outputting their content?

It'd be helpful to know what exactly is returned:

$firstimage = $finalarrayitem->job_image->first();
// get the type of returned value
echo gettype($firstimage);
// if it's an object, see if it's a Pageimage
if (is_object($firstimage)) echo " " . get_class($firstimage);

Of course you could also try doing var_dump($firstimage), but for larger objects that's somewhat difficult to read :)

  • Like 2
Link to comment
Share on other sites

Thanks for welcoming.

I use the function to list multiple pages. Some pages have an image. Some have not. With your code I get two different results:

object Pageimage

boolean

Of course, their are more than 2 Pages...

Could the collision between the pages with image and the pages without cause the failure?

I had tried vardump. A quite little bit too much code... ;)

--- EDIT ---

I praise you teppo!!

If I check, if it is an Object or not, before I call the height-methode, it works fine.

Learning Checklist:

- Learned something important about Processwire Images and Objects :check:

- Erase all comments, which block your output, before you post a answer in the forum, to avoid Post-editing or Doubleposts... :check: ;)

Thanks a lot. You saved literally the future of a whole region. ;)



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