<p>A similar question has already been asked here: <a href="https://craftcms.stackexchange.com/questions/1648/is-there-a-way-to-find-out-if-images-assets-are-landscape-or-portrait">Is there a way to find out if images (assets) are landscape or portrait?</a></p>

<p>You will need to create the two asset transforms you mentioned in your question, and then in your template code use an <code>if/else</code> statement to determine which transform to use, depending on if the asset is portrait or landscape.</p>

<p>Thanks to Josh for the code, this is taken from his answer:</p>

<pre><code>{% set myImage = entry.imagesField.first %}

{% if myImage.getWidth() &lt; myImage.getHeight() %}
  {# portrait #}
{% elseif myImage.getWidth() &gt; myImage.getHeight() %}
  {# landscape #}
{% else %}
  {# square #}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-12-09T16:04:25.310Z
Ah, yes. I was looking for a transformation rather than an in-template solution, but that's fine. Thanks
lowe_22
CC BY-SA 3.0
2015-12-01T13:39:51.777Z
2015-12-01T13:39:51.777Z
2017-04-13T12:51:00.253Z
Jamie Wade
<p>I'm looking to create an image transformation where either the height is (e.g.) 180px, or the width is 180px depending on whether the image is portrait or landscape.</p>

<p>So, if the image is landscape the dimensions should be (WxH): 180 x AUTO
If the Image is portrait, the dimensions should be (WxH): AUTO x 180</p>

<p>Maybe I'm missing something obvious. Or maybe this could be a CSS issue?</p>

<p>Thanks!</p>

<image-transforms>
Image transform with either width or height
# Image transform with either width or height

(Asked by: username_0 on 2015-12-01)

I'm looking to create an image transformation where either the height is (e.g.) 180px, or the width is 180px depending on whether the image is portrait or landscape.

So, if the image is landscape the dimensions should be (WxH): 180 x AUTO
If the Image is portrait, the dimensions should be (WxH): AUTO x 180

Maybe I'm missing something obvious. Or maybe this could be a CSS issue?

Thanks!

**Tags:** image-transforms


---

## 1 answers

### Answer by username_1 on 2015-12-01. Score: 5 | Accepted answer

A similar question has already been asked here: Is there a way to find out if images (assets) are landscape or portrait?
You will need to create the two asset transforms you mentioned in your question, and then in your template code use an if/else statement to determine which transform to use, depending on if the asset is portrait or landscape.
Thanks to Josh for the code, this is taken from his answer:
{% set myImage = entry.imagesField.first %}

{% if myImage.getWidth() < myImage.getHeight() %}
  {# portrait #}
{% elseif myImage.getWidth() > myImage.getHeight() %}
  {# landscape #}
{% else %}
  {# square #}
{% endif %}

> **Comments:**
> **username_0**: Ah, yes. I was looking for a transformation rather than an in-template solution, but that's fine. Thanks  

---

