Categories: Development

Getting image exif data with PHP

Exif stands for Exchangable Image Format, it is the data and information that gets attached to digital images. From location, aperture, IOS, camera, lens, date time, image size + format and so much more the Exif data can tell a lot about the image.

Getting exif data from images using PHP is done with exif_read_data() a vanilla PHP function.

To see all the possible values that are returned run the following on an image that has exif data (taken from documentation example):

$exif = exif_read_data('imagename.jpg', 0, true);
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        echo "$key.$name: $val
\n"; } }

It will return all the possible keys and their values (if the values exist). GPS data as an example wont exist for cameras that dont have this feature.

Here is a code snippet that gets the more basic and fundemental data from exif like: Camera brand, model, date time,  image w x h, ISO, aperture, Shutter speed, lens and focal length

$image_file = 'DSC0001.ARW';
$exif = exif_read_data($image_file, 0, true);
$brand = $exif["IFD0"]["Make"];
$camera = $exif["IFD0"]["Model"];
$software = $exif["IFD0"]["Software"];
$size = $exif["FILE"]["FileSize"];
$date_time = $exif["IFD0"]["DateTime"];
$width = $exif["COMPUTED"]["Width"];
$height = $exif["COMPUTED"]["Height"];
$aperture = $exif["COMPUTED"]["ApertureFNumber"];
$shutter_speed = $exif["EXIF"]["ExposureTime"];
$iso = $exif["EXIF"]["ISOSpeedRatings"];
$focal_length = $exif["EXIF"]["FocalLength"];
$lens = $exif["EXIF"]["UndefinedTag:0xA434"];
$fl_calc = eval('return ' . $focal_length . ';');
$focal_length_mm = "$fl_calc mm";
echo "$file ($height x $width) was shot at $date_time with a $brand $camera at settings of: ISO $iso Shutter speed at $shutter_speed and aperture of $aperture. Focal length was $focal_length_mm and the lens a $lens";


DSC0001.ARW (4000 x 6000) was shot at 2019:02:20 10:29:10 with a SONY ILCE-6500 at settings of: ISO 640 Shutter speed at 1/2500 and aperture of f/5.6. Focal length was 200 mm and the lens a FE 70-200mm F4 G OSS

You can take example of how i retrieve the values by using the array key as seen in the output from the first example above.

There is plenty more values that can be returned so make sure you check it out fully.

You can also process the exif data for all images in a folder:

$dir = '';//'the/path/'
foreach(glob($dir.'*.{arw,ARW,jpg,JPG,jpeg,JPEG,png,PNG}',GLOB_BRACE) as $file){
    $name_only = substr($file, 0, strpos($file, "."));//removes file type (. and everything following)
    $exif = exif_read_data($file, 0, true);
    $brand = $exif["IFD0"]["Make"];
    $camera = $exif["IFD0"]["Model"];
    $software = $exif["IFD0"]["Software"];
    $size = $exif["FILE"]["FileSize"];
    $date_time = $exif["IFD0"]["DateTime"];
    $width = $exif["COMPUTED"]["Width"];
    $height = $exif["COMPUTED"]["Height"];
    $aperture = $exif["COMPUTED"]["ApertureFNumber"];
    $shutter_speed = $exif["EXIF"]["ExposureTime"];
    $iso = $exif["EXIF"]["ISOSpeedRatings"];
    $focal_length = $exif["EXIF"]["FocalLength"];
    $lens = $exif["EXIF"]["UndefinedTag:0xA434"];
    $fl_calc = eval('return '.$focal_length.';');
    $focal_length_mm = "$fl_calc mm";

    echo "$file ($height x $width) was shot at $date_time with a $brand $camera at settings of: ISO $iso Shutter speed at $shutter_speed and aperture of $aperture. Focal length was $focal_length_mm and the lens a $lens 
"; }

Useful if you were inserting these values into a database or water marking images with the exif data.

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago