Rendering with asset() works with png but not with svg image

Hi,

I am trying to use an image which I placed in assets/images in a header.php snippet as a logo. So far, I have come up with a solution looking like this:

<a class="logo" href="<?= $site->url() ?>"

<?= $stlogo = asset('assets/images/ST_Logo.png');

echo $stlogo->resize(150); ?></a>

This works as long as I am using .png. It does not work with .svg although even the Kirby reference site for the asset() function uses svg as an example. What am I doing wrong?

Welcome to the forum, @bene

There are a few syntax issues with your code, but apart from that, creating thumbnails of .svg files doesn’t work (and doesn’t make sense). It probably shouldn’t throw an error either, but just return the file as is, but that’s another issue.

Hi @texnixe and thanks for clarifying!

Would you point those syntax mistakes? I am trying to learn php/kirby. I can make stuff work somehow but, quite obviously, the code isn’t very beautiful…

What confused me to no end is that in the reference, using asset() on an svg is the showcase example: https://getkirby.com/docs/reference/objects/asset Why would you want to do that if not creating a thumbnail from an svg?

<?php
$stlogo = asset('assets/images/ST_Logo.png');
?>
<a class="logo" href="<?= $site->url() ?>"><?=  $stlogo->resize(150)->html(); ?></a>

Because the asset method has other useful methods for dealing with files that are located outside of the content folder.

These are the resizable file types:

I would change the code like this for resizable files:

<?php 
$stlogo = asset('assets/images/ST_Logo.png');
if ($stlogo->exists()): ?>
  <a class="logo" href="<?= $site->url() ?>"><?= $stlogo->resize(150)->html() ?></a>
<?php endif ?>

And for non-resizable files:

<?php 
$stlogo = asset('assets/images/ST_Logo.svg');
if ($stlogo->exists()): ?>
  <a class="logo" href="<?= $site->url() ?>"><?= $stlogo->html() ?></a>
<?php endif ?>
  1. <a class="logo" href="<?= $site->url() ?>": The closing > is missing
  2. <?= $stlogo = asset('assets/images/ST_Logo.png');: You are echoing the variable definition

A code editor which shows syntax errors is gold.

Thanks @texnixe and @bnomei !