How do you use asset urls?

When having images as assets I usually load them like this:

<img src="<?php echo u() . '/assets/images/image.png'; ?>">

An alternative would be this, but it’s longer:

<img src="<?php echo kirby()->urls()->assets() . '/images/image.png'; ?>">

Today I learned from this post that it’s also possible to create an object and use that instead. It’s longer but it’s possible to do things like resize and crop the asset image.

<?php $image = new Asset('assets/images/image.png'); ?>
<img src="<?php echo $image->url(); ?>">

What version would you use? And why?

2 Likes

There’s another option I usually use:

<?php echo url('assets/images/image.png') ?>

I’d use the new Asset class if I need an image object.

4 Likes

That’s looking much better yes. :slight_smile: