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…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago