User Tools

Site Tools


gps:start

GPS

PHP Code

public function getGps($exifCoord, $hemi) {
 
    $degrees = count($exifCoord) > 0 ? gps2Num($exifCoord[0]) : 0;
    $minutes = count($exifCoord) > 1 ? gps2Num($exifCoord[1]) : 0;
    $seconds = count($exifCoord) > 2 ? gps2Num($exifCoord[2]) : 0;
 
    $flip = ($hemi == 'W' or $hemi == 'S') ? -1 : 1;
 
    return $flip * ($degrees + $minutes / 60 + $seconds / 3600);
 
}
 
public function gps2Num($coordPart) {
 
    $parts = explode('/', $coordPart);
 
    if (count($parts) <= 0)
        return 0;
 
    if (count($parts) == 1)
        return $parts[0];
 
    return floatval($parts[0]) / floatval($parts[1]);
}
// Sample Usage
$exif = exif_read_data($filename, 'GPS', true, false);
$lon = getGps($exif['GPS']['GPSLongitude'], $exif['GPS']['GPSLongitudeRef']);
$lat = getGps($exif['GPS']['GPSLatitude'], $exif['GPS']['GPSLatitudeRef']);
var_dump($lat, lon);
 
float(-33.8751666667)
float(151.207166667)

Exif (Exchangeable Image File Format)

The Japan Electronic Industries Development Association published Version 2.1 of the specification in June 1998 with an initial specification release in 1995. Yes, that is correct. The 1998 version is 2.1, which is what many hardware manufacturers are (somewhat) implementing today in devices such as cameras, scanners, and smartphones!

Now, the Exif metadata is stored in the APP1 marker of JPG files, so if you are looking at the picture in a hex/text editor, you can search for segment marker 0xFFE1.

Tips

  • Each hardware manufacturer may implement Exif GPS data differently, so you will have to check the model and perform different calculations for each device to convert to latitude and longitude.
  • If your device operates as expected, you will have to enable GPS communication before your pictures will have the Exif metadata embedded in it.
  • The accuracy of GPS data within the picture relies on a few things: your calculation, the device's ability to pinpoint your location, and the implementation your hardware manufacturer took.
  • The JPEG/Exif format is one of the most commonly saved file types of digital cameras.
  • Make sure mb_string is loaded before Exif in your php.ini.
  • If you are on Windows XP, know that copying an image from your camera device may inadvertently remove Exif data from the picture.
  • When using the Google Maps APIs, be sure to note that south and west are negative values.

Warnings

  • Exif metadata inside of JPEG is restricted in size to 64 kB because the information must be contained within a single JPEG APP1 segment.
  • Exif data may disappear or become corrupt if you modify the picture in photo editing software or simply copy it from your device to the computer on some OSs.

References

gps/start.txt · Last modified: 2013/11/11 13:21 by nejo