Forum

Thread tagged as: Question

Conditional based on image dimensions (width or height)

Hello,

Is it possible within a template to have a conditional based on the dimensions of an image?

I've tried this to only show a block if an image is wider than 1280px, which didn't work:

<perch:if "image" output="w" match="gt" value="1280">

No luck with this either:

<perch:if "image" format="w" match="gt" value="1280">

(There's a 404 Error when accessing the doc for "Formats" https://docs.grabaperch.com/docs/content/template-tags/format/ from this page https://docs.grabaperch.com/docs/templates/conditionals/if/)

Thanks for any pointers.

Stéphane Mégécaze

Stéphane Mégécaze 0 points

  • 6 years ago

Stephane, I am curious why you would have to test the size of an image in a template since the image size would be controlled in your template. Of course I don't know how your image's are coming into play here. :)

Anyways, I am not sure but this might be the way...

<perch:if id="image" match="gt" value="{image}">

OR

<perch:if id="image" match="gt" value="1280">

this assumes your image tag else where in the template is output="w"

RK

NOTE: untested

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't test against the dimensions of an image.

Thanks Robert for the suggestions, and thank Drew for the fast reply.

Robert: The use case is a page to download pictures in small and full size. Since we don't know how large the pictures will be, we wanted to hide links to the full size picture if the small size is the largest available.

Stephane, This is likely something you would have to test the output in PHP, then send it back to perch for further processing.

RK