EXIF in Craft CMS: Accessing and displaying EXIF in Twig templates

Published:
Tagged:
craftcms

Image EXIF data can be accessed directly in Twig templates using Craft CMS. The following settings are required to make sure EXIF is preserved and accessible.

Config and settings #

EXIF data from image files can be accessed and displayed if the following things are taken into consideration:

PHP EXIF extension

It’s worth checking that your environment has the PHP EXIF extension installed. Despite Craft showing PHP EXIF as installed, I wasn’t having any luck retrieving the EXIF until I installed the extension through Craft Nitro

Finding the right path #

You need to provide the full path to the original image in order to retrieve the EXIF data. Depending on how the asset folder is setup, you may need to amend the path.

Returning sample EXIF #


{# Get a sample image containing EXIF data #}
{% set image = craft.assets.filename('exif-example.jpg').one() %}
{# Create variable using retrieved EXIF from image #}
{% set getExif = craft.app.images.getExifData("." ~ image.url) %}
{% dd(getExif) %}

Use case #

Displaying EXIF can be useful for providing context to an image through date, time and even GPS co-ordinates. The original motivation for experimenting with this was fujiframe.com. Like may photography sites, readers have take an interest in the settings used to capture an image. You can see examples of the EXIF in use on Fujiframe.com

Example output


[
    'uri' => './assets/images/fuji-90mm-f2.jpg'
    'filepath' => '/app/web/assets/images/fuji-90mm-f2.jpg'
    'file.FileName' => 'fuji-90mm-f2.jpg'
    'file.FileDateTime' => 1639563705
    'file.FileSize' => 1734553
    'file.FileType' => 2
    'file.MimeType' => 'image/jpeg'
    'file.SectionsFound' => 'ANY_TAG, IFD0, EXIF'
    'computed.html' => 'width=\"4000\" height=\"2666\"'
    'computed.Height' => 2666
    'computed.Width' => 4000
    'computed.IsColor' => 1
    'computed.ByteOrderMotorola' => 1
    'computed.CCDWidth' => '21mm'
    'computed.ApertureFNumber' => 'f/4.0'
    'computed.Copyright' => 'JOE SPITERI                                                                                                                                                                                                                                                    '
    'ifd0.Make' => 'FUJIFILM'
    'ifd0.Model' => 'X-T4'
    'ifd0.XResolution' => '300/1'
    'ifd0.YResolution' => '300/1'
    'ifd0.ResolutionUnit' => 2
    'ifd0.Software' => 'Capture One 20 Macintosh'
    'ifd0.DateTime' => '2021:05:17 20:14:53'
    'ifd0.Artist' => 'JOE SPITERI'
    'ifd0.Copyright' => 'JOE SPITERI                                                                                                                                                                                                                                                    '
    'ifd0.Exif_IFD_Pointer' => 480
    'exif.ExposureTime' => '1/5'
    'exif.FNumber' => '4/1'
    'exif.ExposureProgram' => 1
    'exif.ISOSpeedRatings' => 160
    'exif.UndefinedTag:0x8830' => 1
    'exif.ExifVersion' => '0230'
    'exif.DateTimeOriginal' => '2021:05:16 16:47:07'
    'exif.ShutterSpeedValue' => '9297/4004'
    'exif.ApertureValue' => '4/1'
    'exif.BrightnessValue' => '-7/100'
    'exif.ExposureBiasValue' => '0/1'
    'exif.MaxApertureValue' => '2/1'
    'exif.MeteringMode' => 1
    'exif.LightSource' => 0
    'exif.Flash' => 0
    'exif.FocalLength' => '50/1'
    'exif.ColorSpace' => 1
    'exif.ExifImageWidth' => 4000
    'exif.ExifImageLength' => 2666
    'exif.FocalPlaneXResolution' => '61669376/32768'
    'exif.FocalPlaneYResolution' => '61669376/32768'
    'exif.FocalPlaneResolutionUnit' => 3
    'exif.SensingMethod' => 2
    'exif.FileSource' => ''
    'exif.SceneType' => ''
    'exif.ExposureMode' => 1
    'exif.WhiteBalance' => 1
    'exif.FocalLengthIn35mmFilm' => 75
    'exif.SceneCaptureType' => 0
    'exif.Sharpness' => 0
    'exif.SubjectDistanceRange' => 0
    'exif.UndefinedTag:0xA431' => '0CQ09248'
    'exif.UndefinedTag:0xA432' => [
        0 => '5000/100'
        1 => '5000/100'
        2 => '2/1'
        3 => '2/1'
    ]
    'exif.UndefinedTag:0xA433' => 'FUJIFILM'
    'exif.UndefinedTag:0xA434' => 'Fujifilm Fujinon XF50mmF2 R WR'
    'exif.UndefinedTag:0xA435' => '9CB02589'
]