Chaining Image Effects

I’m trying to chain two image effects, crop and bw. I assumed it should be working by using either

<?php echo $item->coverimage()->toFile()->crop(2000, 400, 90)->bw() ?>

or

<?php echo $item->coverimage()->toFile()->crop(2000, 400, 90)->toFile()->bw() ?>

But it doesn’t work - I get no output at all. What am I doing wrong?

If you want to use multiple image mods, you can use the thumb() method:

Don’t forget to check if you have an object before you start stringing methods! Your code above is not really safe.

I’ve shortened the code a bit…

<?php if ($image = $item->coverimage()->toFile()) : ?>
   <?php echo $item->coverimage()->toFile()->crop(2000, 400, 90)->bw() ?>
<?php endif;

This would be safer, is it? Thanks for the hint to the thumb object.

Well, almost, but once you have defined a variable, you might as well use it:

<?php if ($image = $item->coverimage()->toFile()) : ?>
   <?php echo $image->thumb(['grayscale' => true, 'width' => 2000, 'height' => 400, 'crop' => true]) ?>
<?php endif; ?>
2 Likes

Thanks for the suggestion!