Resize image by longest side

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?

Thanks!

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

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

1 Like

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 ?>

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

<?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>
2 Likes

This is awesome!
Thank you :smiley: