Crop not working vertically

I’m trying to crop the first image in a page to a square with something like
$page->image()->crop(350)
and don’t get the expected result: the image is resized to 350 horizontally, but is still higher vertically. The original ratio is kept.
I tried the more explicit crop(350, 350) without success. I tried with both ‘gd’ and ‘im’ as ‘thumbs.driver’, same result.

Any idea why the crop wouldn’t work?

This is a little strange … Could you upload the original image file please, so I can try to recreate your issue?

Does it work with thumb?

echo thumb($page->image(), array('width' => 350, 'crop' => true));

@Thiousi: to crop an image using the thumb method, you would have to use the crop option:

echo thumb($image, array('width' => 300, 'crop' => true));

I edited my post just a few seconds before your comment I believee :wink:

It tried with other images, same issue, so the problem is in my code.

What I’m doing is a foreach loop:

<?php foreach($page->children()->filterBy('template', 'intervenants')->children()->visible()->shuffle()->limit(3) as $intervenant): ?>
  <img src="<?php echo $intervenant->image()->crop(350)->url() ?>" alt="">
<?php endforeach ?>

And guess what? As I’m typing this it suddenly works with my initial code and image, I really don’t understand why, and I feel even dumber :open_mouth:

Can’t reproduce the crop issue now… Weird, but I guess this topic can be flagged as ‘solved’. Sorry about that!

I don’t see any problem with your code actually :slight_smile:
If the image you want cropped is selected in a field called image in the children of pages with the template intervenants that is! (I love Kirby for how you were able to write that foreach…)

Ok I can reproduce it after all:

Same image. The only difference is that I fiddled with / replaced the top image several times.
If I switch to a horizontal image it suddenly works:

Using a vertical image doesn’t.

I still don’t know why it suddenly worked for the first vertical image :confused:

Edit: I re-uploaded the street image (the one that suddenly worked) and boom. Crop is wrong again.

Hm, I just tested this with a few vertical images from unsplash in a loop and no problems. Are you using a lot of images?

$page->image() without a parameter gets the first image in page folder, not an image from a field.

Thumb created with the wrong size. I also tried an image from Unsplash, same result.

I don’t have lots of images, I only have 3 test contents right now, each with a single image.

Damn right! And I’ve made that mistake on several occasions in the past !

What is the result if you use the thumb method with the crop option as suggested by @Thiousi?

Same result with the crop option. I still don’t understand how it suddenly worked briefly :confused:

Edit: scrap that, if I switch to gd it works, so it’s an issue with imagick!

But you wrote earlier that gd also has this issue sometimes. So maybe it’s something else.

What happens if you print out the imagick command in the thumb driver? Is that consistent every time or does it contain different params when it works and when it doesn’t?

I tried a lot of different things before I opened the topic, but in a not-so organized way, so I think I have mistakenly concluded in didn’t work with gd. Once a thumb is created you have to remember to delete it every time, otherwise it isn’t recreated even if you switch the thumb driver. I initially didn’t always delete it.

After more tests I can confirm it works with gd, not with imagick even when I use
echo thumb($image, array('width' => 300, 'crop' => true));

More findings: turns out an easy temporary fix is to use ->crop(350, 350). If the height is set explicitly, no problem.

Interesting. Have you tried dumping the imagick command in the thumb driver as I suggested? That might help with debugging.

How do I do that? :blush:

Another thing I noticed is the ‘gd’ driver uses a fitWidthAndHeight function that doesn’t seem to have an equivalent in the crop code in the ‘im’ driver.
So around line 330 I added a quick n’dirty bit that seemed to do the trick:

if ($thumb->options['height'] == null) {
  $thumb->options['height'] = $thumb->options['width'];
}

Ha, dump():

Array
(
    [0] => convert
    [1] => "/Users/xxx/Documents/Sites/tedxsaclay/contents.git/content/(…)/_r003566.jpg"
    [2] => -strip
    [3] => -resize
    [4] => 350x^
    [5] => -gravity Center -crop 350x+0+0
)
1 Like