I had a situation in my current project where I wanted to add img’s to my gallery template differently depending on whether they were portrait or landscape. But there are no built-in actions to determine the width and height of an image file. The width() and height() actions are only to set the width or height.
What I have done for now is to edit the ImageMedium.php file (/system/src/Grav/Common/Page/Medium/ImageMedium.php) by adding these two methods:
—php
public function getWidth()
{
if (!$this->image) {
$this->image();
}
return $this->image->getAdapter()->width();
}
public function getHeight()
{
if (!$this->image) {
$this->image();
}
return $this->image->getAdapter()->height();
}
Now I can call them from twig like this:
{% if page.media[painting.file].getHeight() > page.media[painting.file].getWidth() %}
{% set portrait = true %}
{% else %}
{% set portrait = false %}
{% endif %}
This is just fine and dandy, except I have to update that file every time grav updates. Is there a simpler, or at least update-proof way of doing this? Thanks.