# Resize image by longest side

**URL:** <https://forum.getkirby.com/t/resize-image-by-longest-side/17838>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 10, 2020, 9:31am UTC](https://forum.getkirby.com/t/resize-image-by-longest-side/17838 "2020-04-10T09:31:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [April 10, 2020, 9:31am UTC](https://forum.getkirby.com/t/resize-image-by-longest-side/17838/1 "2020-04-10T09:31:52Z")

</div>

Hello!

I want to resize my gallery images to a certain size.  
I tried to resize my images with the following:

resize by height  
$image-\>resize(null, 200);

resize by width  
$image-\>resize(300);

But now I am resizing images with portrait and landscape position. Is it possible to resize a image by the longest side of the image? For example. Portrait (vertical) images by height and landscape (horizontal) images by width?

> **[$file-\>resize()](https://getkirby.com/docs/reference/objects/cms/file/resize)**
>
> Resizes the file with the given width and height while keeping the aspect ratio.

Thanks!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 10, 2020, 9:34am UTC](https://forum.getkirby.com/t/resize-image-by-longest-side/17838/2 "2020-04-10T09:34:11Z")

</div>

> [@portobien](#):
>
> But now I am resizing images with portrait and landscape position. Is it possible to resize a image by the longest side of the image?

You can check the image ratio and then act depending on that.

> **[$dimensions-\>ratio()](https://getkirby.com/docs/reference/objects/image/dimensions/ratio)**
>
> Calculates and returns the ratio

Or use `$image->isLandscape()`/`$image->isPortrait()`

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [April 10, 2020, 9:49am UTC](https://forum.getkirby.com/t/resize-image-by-longest-side/17838/3 "2020-04-10T09:49:40Z")

</div>

Thanks!  
So if I am right is should be:

```
<?php foreach ($gallery as $image): ?>
    <div class="isotope-item gallery-item">
        <div class="gallery-item-inner item-inner">

            <?php if($image->isLandscape): ?>
                <a href="<?= $image->url() ?>" class="thumb-hover scale" data-rel="lightcase:gallery1">
                    <img class="lazy" data-src="<?= $image->isLandscape->resize(600)->url() ?>" alt="<?= $image->alt() ?>">
                  </a>
                <?php else: ?>

                    <a href="<?= $image->url() ?>" class="thumb-hover scale" data-rel="lightcase:gallery1">
                    <img class="lazy" data-src="<?= $image->isPortrait->resize(null, 600)->url() ?>" alt="<?= $image->alt() ?>">
                  </a>
                    <?php endif ?>

        </div>
    </div>
    <?php endforeach ?>
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 10, 2020, 9:54am UTC](https://forum.getkirby.com/t/resize-image-by-longest-side/17838/4 "2020-04-10T09:54:36Z")

</div>

No there’s a syntax error and too much repetitive code:

```auto
<?php 
 
$resizeUrl = $image->isLandscape() ? $image->resize(600)->url() : $image->resize(null, 600)->url();

<a href="<?= $image->url() ?>" class="thumb-hover scale" data-rel="lightcase:gallery1">
   <img class="lazy" data-src="<?= $resizeUrl ?>" alt="<?= $image->alt() ?>">
</a>

```

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [April 10, 2020, 10:06am UTC](https://forum.getkirby.com/t/resize-image-by-longest-side/17838/5 "2020-04-10T10:06:36Z")

</div>

This is awesome!  
Thank you 😃
