Read EXIF data from images

Good Morning,

can someone give me an example how I insert specific EXIF Data (like aperture and ISO)?

Kind regards, Simon

Not really sure what you mean. Could you please clarify? Do you want to read exif data from your images? Or add EXIF data to your files?

Kirby allows you to add metadata (separate text files attached to each media file). There is no out-of-the box way to write EXIF data to images.

Yes, I want to read the exif data from the images and place them as text below.


It should look like this under the image.

You can get the exif data with $image->exif() ($image->exif() | Kirby CMS). This returns an Exif object, which you can query for details:

Can you give an example how that would look in code? I mean how I would query that object?
Thanks.

if ($exif = $image->exif()) {
  echo $exif->aperture();
}
1 Like

Thank you.

One last question:
Can you tell me, why it gives “1590/10” and not “159” for the focal length? How can I change that?

Because that is what PHP returns for that value. You’d have to convert it to the output you want. In this case, divide the two values you get when exploding the string.

Since you probably want to get the focal length of more than one image, better add a function in a plugin’s index.php, e.g.

function parseFocalLength(?string $focalLength): string
{
    if (is_null($focalLength)) {
        return '';
    }

    [$a, $b] = explode('/', $focalLength);

    if ($a && $b && $b !== 0) {
        return $a/$b . ' mm';
    }

    return '';
}

Then in your template

<?php
if (($image = $page->image()) && $exif = $image->exif()) {
  echo parseFocalLength($exif->focalLength());
}
1 Like

Thank you very much! :slight_smile: