Get an image without specifying extension

Hello all,

I’m trying to load an image but regardless of the image extension. So if the user upload a profil picture, it’s not a problem if it is jpg, png, or any image file format.

Here is an example of what I try to achieve :

$image = $page->image->('profil.jpg'); // Here we need to specify extension
if (!$image)
	$image = $page->image->('profil.png');
if (!$image)
	$image = $page->image->('profil.gif');
// ...

Is there a way to do it with Kirby ? I search in the doc but the best thing I found is to use :

$image = $page->images(); 

So we only have the image files but how to get one with specific name ?

Thanks in advance

I guess you might want to use something like this:

$files = $page->files()->filterBy('filename', '*=', 'profil');
// check if something was found and then:
$image = $files->first();

Why not use a files field with the max set to one (so they can only upload 1 image to it).

It wont matter what the extension is then. I think its unsafe to assume they will always use the correct name for the file, which makes the filterby method above problematic.

Yes, I thing it’s exactly what I was searching for. Thank you.

The problem is that it would probably be others images in the users folder :worried:
For the filterBy method, i though that it would be possible to automatically change the file name when it’s upload in the panel. I’m new to Kirby so I don’t really know what is possible or not.

Ty for your response.

I just answered you with the exact same thing as @jimbobrjames did without seing that answer, so I deleted it.

The nice thing about the image field ist, that you can set it to multiple=false. This way only one image can be selected. you can then just use that field in your tempalte:

<?php if($image = $page->IMAGEFIELD()->toFile()): ?>
  <img src="<?= $image->url() ?>" alt="">
<?php endif ?>

This way you dont have to hardcode any filenames into your template. You can have multiple images uploaded to the page/user/whatever but only one can be selected for that specific output

So if i understand, when the user upload an image, the image name is stored in a field ?

Yes.

Ok ok ty :smiley: