Crop a user image/avatar

Hi. Hasn’t anyone ever tried to crop an avatar? $user->avatar()->crop(500)->url() doesn’t seem to work. Please advise.

What does that mean? You don’t get any output? The image is not cropped? You get an error message (if yes, what is it)? Anything else?

Like with all objects, never forget to wrap your code in an if statement:

<?php if(($user = $kirby->user()) && ($avatar = $user->avatar())): ?>
   <img src="<?= $avatar->crop(100)->url() ?>" alt="">
<?php endif; ?>

Hi @texnixe, Thanks for the response. It outputs the original profile image. No errors. It just doesn’t crop the image. Here’s a look at the code, simplified.

<?php 
$items = $page->mems()->toStructure(); 
foreach($items as $item):
  if($user = $item->mem()->toUser()): 
    if($img = $user->avatar()): 
      $img_path = $img->crop(500)->url();
    else:
      $img_path = 'fallback.jpg';
    endif ?>
    <img src="<?= $img_path ?>" alt="">
  <?php endif; 
endforeach 
?>

Actually, it is not the original profile image. It is a regenerated image, but it’s just not cropped.

Wait… no image is cropping on this site. I tried to just use crop() on a simple standalone image and got the same results. Let me investigate this further, update my install, etc.

That’s was going to be my next question… :wink:

Check if GD is enabled on the server ( I guess resizing doesn’t work either?), or if you have set the thumb driver to im, if imagemagick is installed.

@texnixe, I was running the latest version of the Focus plugin on a 3.5.6 Kirby install. I updated the installed to the latest, 3.6.1.1, and we’re all good now. Ugh. Thanks for stickin’ with me through this confusion.