Skip to content

Instantly share code, notes, and snippets.

@hastinbe
Last active September 13, 2019 01:37
Show Gist options
  • Save hastinbe/6ba47b243da1adcd063ce64a25ca1c0c to your computer and use it in GitHub Desktop.
Save hastinbe/6ba47b243da1adcd063ce64a25ca1c0c to your computer and use it in GitHub Desktop.
Gets EXIF GPS data from an image #php #gps
<?php
/**
* GPS helper class.
*
* @package Gps
*
* @author Beau Hastings <[email protected]>
* @copyright 2019 Beau Hastings
* @license GNU GPL v2
*
* @link https://gist.github.com/hastinbe/6ba47b243da1adcd063ce64a25ca1c0c
*/
namespace hastinbe\Gps;
class Gps
{
/**
* EXIF data.
*
* @var array
*/
protected $exifData = [];
/**
* Image path.
*
* @var string
*/
protected $path = '';
/**
* Create a new instance.
*
* @param string $path Path to image.
* @param bool $read Read EXIF data on instance creation
*/
public function __construct(string $path, $read = false)
{
$this->path = $path;
if ($read) {
$this->readExifData();
}
}
/**
* Get EXIF data.
*
* @return mixed
* @throws \Exception
*/
public function getExifData($attribute = null)
{
if ($attribute === null) {
return $this->exifData;
}
if (!isset($this->exifData[$attribute])) {
throw new \Exception("EXIF attribute '$attribute' is undefined");
}
return $this->exifData[$attribute];
}
/**
* Set EXIF data.
*
* @param array $exif
* @return Gps
*/
public function setExifData(array $exifData)
{
$this->exifData = $exifData;
return $this;
}
/**
* Get path to image.
*
* @return string
*/
public function getPath(): string
{
return $this->path;
}
/**
* Set path to image.
*
* @param string $path
* @return Gps
*/
public function setPath(string $path)
{
$this->path = $path;
return $this;
}
/**
* Read EXIF data.
*
* @return Gps
*/
public function readExifData()
{
$this->setExifData(exif_read_data($this->getPath()));
return $this;
}
/**
* Get coordinate in degrees.
*
* @param array $coordinate
* @param string $hemisphere For latitude, N or S; for longitude, E or W
* @return float
*/
public function getCoordDegrees($coordinate, $hemisphere): float
{
for ($i = 0; $i < 3; $i++) {
$part = explode('/', $coordinate[$i]);
if (count($part) == 1) {
$coordinate[$i] = $part[0];
}
elseif (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);
}
/**
* Get the latitude and longitude.
*
* @return array
*/
public function getLatLong(): array
{
$latitude = $this->getCoordDegrees(
$this->getExifData('GPSLatitude'),
$this->getExifData('GPSLatitudeRef')
);
$longitude = $this->getCoordDegrees(
$this->getExifData('GPSLongitude'),
$this->getExifData('GPSLongitudeRef')
);
return [$latitude, $longitude];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment