Development

GoPro Image Exif data with PHP

Here is how to get a GoPro’s image data (EXIF) such as size, settings, configuration and location using PHP and exif_read_data().

I have already covered getting image EXIF data in PHP see here so the concept is exactly the same except a GoPro has more/different values in the returned data than a regular camera would.

Simply grab an image taken from a GoPro define it at $image_file = ''; and run the below PHP code to see all the available EXIF data:

$exif = exif_read_data($image_file, 0, true);
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        echo "$key.$name: $val<br />\n";
    }
}

The GPS data (if the GoPro had it turned on) is formatted in extra array’s and not in simple Latitude x Longitude format.

With the help of a function found here it can easily get turned into a useable format.

Running the code below the data returned was

PHP GoPro EXIF data

This can be helpful if you wanted to create an image database, compress your photos and make a thumbnail by executing a for all jpg in folder.

Full code with output:

<?php
$image_file = 'GOPR1280.jpg';

$exif = exif_read_data($image_file, 0, true);
$file_name = $exif["FILE"]["FileName"];
$model = $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"];
$contrast = $exif["EXIF"]["Contrast"];
$saturation = $exif["EXIF"]["Saturation"];
$sharpness = $exif["EXIF"]["Sharpness"];
$white_balance = $exif["EXIF"]["WhiteBalance"];
$digital_zoom_ratio = $exif["EXIF"]["DigitalZoomRatio"];
$fl_calc = eval('return ' . $focal_length . ';');
$focal_length_mm = "$fl_calc mm";

$exif2 = exif_read_data($image_file);
$latitude = gps($exif2["GPSLatitude"], $exif2['GPSLatitudeRef']);
$longitude = gps($exif2["GPSLongitude"], $exif2['GPSLongitudeRef']);

function gps($coordinate, $hemisphere)//@David Jones StackOverflow
{
    if (is_string($coordinate)) {
        $coordinate = array_map("trim", explode(",", $coordinate));
    }
    for ($i = 0; $i < 3; $i++) {
        $part = explode('/', $coordinate[$i]);
        if (count($part) == 1) {
            $coordinate[$i] = $part[0];
        } else if (count($part) == 2) {
            $coordinate[$i] = floatval($part[0]) / floatval($part[1]);
        } else {
            $coordinate[$i] = 0;
        }
    }
    list($degrees, $minutes, $seconds) = $coordinate;
    $sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
    return $sign * ($degrees + $minutes / 60 + $seconds / 3600);
}

function format_bytes($bytes)//@Adnan StackOverflow
{
    if ($bytes >= 1073741824) {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    } elseif ($bytes >= 1048576) {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    } elseif ($bytes >= 1024) {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    } elseif ($bytes > 1) {
        $bytes = $bytes . ' bytes';
    } elseif ($bytes == 1) {
        $bytes = $bytes . ' byte';
    } else {
        $bytes = '0 bytes';
    }
    return $bytes;
}
$size_formatted = format_bytes(filesize($file_name));

echo "$file_name ($width x $height) $size_formatted was shot at $date_time with a $model<br>";
echo "ISO $iso Shutter speed at $shutter_speed and aperture of $aperture. Focal length was $focal_length_mm<br>";
echo "Contrast: $contrast Digital Zoom ratio: $digital_zoom_ratio Saturation: $saturation Sharpness: $sharpness White Balance: $white_balance<br>";
echo "LAT:$latitude LONG:$longitude <br>";
echo "Software: $software";

 

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