Jump to content

Image not responding to size()


statestreet
 Share

Recommended Posts

In a template, I'm drawing a 4x3 table that I want to display images from other pages selected from page fields inside a repeater on this page. Their position relative to each other is important, and most of the space in the table will usually be intentionally empty, which is why I'm doing it in this rather strange way:

In the admin, the referenced page's image's position in the table is chosen from page selects with columns and rows defined in /global/matrix/rows/ and /global/matrix/cols/. In the template, we get the images by iterating through the table and seeing if any of the "ingredient" items on this page match the the coordinates of the current table cell.

If what I've described here sounds confusing, it's confusing me, too. :)

Here's my loop:

foreach ($pages->get("/global/matrix/rows/")->children as $row) {
echo '<tr>';
foreach ($pages->get("/global/matrix/cols/")->children as $col) {
  $tdobject = $page->ingredients->find("m_row=$row, m_col=$col")->first()->ingredient;
 echo '<td><img src="'. $tdobject->icon->size(40, 40)->url . '"/></td>'; // line that throws the error
}
echo '</tr>';
}

This gets me:

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

However, changing that line to this works:

echo '<td><img src="'. $tdobject->icon->url . '"/></td>';

So I can pull a URL from the image object on the referenced page, but I can't call size() on it. What am I missing?

Link to comment
Share on other sites

This single vs. multiple has come up several times with images. I should probably have the Pageimages (multi) class look for size() and throw a specific error that answers it.

---

Edit: I also recommend adding this:

if($tdobject->icon) { /* then resize */ }

or if a multi-image field:

if(count($tdobject->icon)) { /* then resize */ }
  • Like 2
Link to comment
Share on other sites

I was pretty sure I had set the icon field to single-image only, but I gave the first() selector a try and instead got this:

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

I get the error about size() as well when I try slkwrm's broken-apart selectors.

I must have overlooked something somewhere else that's affecting this in an unexpected way... any ideas where I should start looking? One new thing I noticed: While the $tdobject->icon->url does resolve to an image that displays in the browser, it only displays when I'm logged in as an admin. The path only renders to HTML when logged in; otherwise it's just <img src=""/>. Weird.

Link to comment
Share on other sites

This single vs. multiple has come up several times with images. I should probably have the Pageimages (multi) class look for size() and throw a specific error that answers it.

That would be great. But I think this is still little problematic. I think image fields should always return arrays, even if you only want to keep single image. That is because I think that is probably only place where tiny change in field settings ends up with errors. Perfect solution would of course be this (not sure if this is possible):

$page->imagefield // returns array of images
$page->imagefield->size(200,200) // returns first image in 200x200 size

So if you don't loop or specifically say that you want other than first image, methods will always take first image. This way images would just work.

  • Like 1
Link to comment
Share on other sites

That's just it, though. There's definitely an image! It's definitely a single-image field, and there's definitely an image there, because $tdobject->icon->url resolves to an image that loads in the browser (albeit only when logged in as admin, which must be a clue). I just can't call size() on it.

Link to comment
Share on other sites

So I can pull a URL from the image object on the referenced page, but I can't call size() on it. What am I missing?

Any chance that it might be a 'file' rather than 'image' field? That would explain the lack of a size() function.

Link to comment
Share on other sites

However, changing that line to this works:

echo '<td><img src="'. $tdobject->icon->url . '"/></td>';

So I can pull a URL from the image object on the referenced page, but I can't call size() on it. What am I missing?

Is the url the correct image url? /site/assets/files/...

foreach ($pages->get("/global/matrix/rows/")->children as $row) {
echo '<tr>';
foreach ($pages->get("/global/matrix/cols/")->children as $col) {
  $tdobject = $page->ingredients->find("m_row=$row, m_col=$col")->first()->ingredient;
 echo '<td><img src="'. $tdobject->icon->size(40, 40)->url . '"/></td>'; // line that throws the error
}
echo '</tr>';
}

If you really can call url on the object but not do a size() on it have you tried debug mode on?

Also can you do a var_dump on the image object and see what you got

if($tdobject) var_dump($tdobject);

Strange but I think I got once a similar issue, but can't recall what it was at the end.

I'm starring at your post and code trying to figure out what you're actually doing here with your setup and code. I can't really decipher it in my head right now how this really makes sense all. Would you be able to make a profil export https://github.com/ryancramerdesign/ProcessExportProfile of your PW site and post that so one could try out locally? Or explain a little more about the exact setup so one could possibly reproduce?

Also are you using very latest commit? I don't know if it may a issue with repeater fields which are still pretty fresh.

Link to comment
Share on other sites

  • 6 months later...

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

×
×
  • Create New...