Image crop not working correctly on portrait images

Hi there,

Trying to use image crop to crop images to a specific set of dimensions. It works fine on landscapes images, but portrait images are not cropping correctly. When I inspect the image in the browser console, it looks as if it’s resizing the image and switching the width to be the height and the height to be the width in terms of crop. The filename is correct, for instance:


But the image itself is actually 375x500 pixels. Instead of the expected 500x375 pixels. Am I missing something? My template code looks like:

<?php foreach($page->files()->sortBy('sort', 'asc') as $file): ?>
          <?php if($file->type() == 'image'): ?>
            <div class="photo">
            <a data-fslightbox href="<?= $file->url() ?>">
                <img src="<?= $file->crop(500,375)->url() ?>" alt="<?= $file->alt() ?>" />
              <?php if ($file->caption()->isNotEmpty()): ?><figcaption><?= $file->caption() ?></figcaption><?php endif ?>
          <?php endif ?>
        <?php endforeach ?>

Is this an image taken on a digital camera or smartphone? There was an issue sometime back with EXIF data storing the dimensions incorrectly, causing Kirby to treat portrait as landscape or vice versa.

Seems to still be open although a linked PR at the bottom should have closed it :person_shrugging:

1 Like

See also: Cropping images from portrait to landscape - #5 by svenatkins

Or maybe do a search here for wrong orientation


Ok so my solution here was to take the clients images, run them through Photoshop and re-save them as jpgs and optimise them. I think as some were taken on a phone as mentioned above, that it was causing some weird issues. It seems to have resolved now that I re-saved them.

Previously I used a windows built-in tool to batch resize them, but I think it kept the exif data which might of been the problem. Either way, for future reference this did the trick.

1 Like

Thank you for this. You put me on the right tracks. I ended up just batch resizing and re-saving them out which I think cleared the exif data from the phone photos.

I had a similar issues with uploading scanned film photos I had gotten from a lab. I resized them with XnConvert which seemed to also fix the metadata and now they resize correctly in Kirby.