Does anyone know if there is a way to read geotag data from photos in PHP?
Thanks
The exif_read_data() function in PHP, helps to read the EXIF headers from an image file. We will use exif_read_data() function to get the GPS data from image.
The Imagick::getImageProperties() function is an inbuilt function in PHP which is used to get the image properties.
The getCurrentPosition() method is used to get the visitor's position and showLocation() method is used to getting the visitor's address from the getLocation. php file using Ajax. HTML Code: After getting the visitor position, the address will be shown on the web page ( #location span).
Will Coughlin's answer is correct though I formulated a function for quick reference in case someone stumbles upon the same problem.
/**
* Returns an array of latitude and longitude from the Image file
* @param image $file
* @return multitype:number |boolean
*/
function read_gps_location($file){
if (is_file($file)) {
$info = exif_read_data($file);
if (isset($info['GPSLatitude']) && isset($info['GPSLongitude']) &&
isset($info['GPSLatitudeRef']) && isset($info['GPSLongitudeRef']) &&
in_array($info['GPSLatitudeRef'], array('E','W','N','S')) && in_array($info['GPSLongitudeRef'], array('E','W','N','S'))) {
$GPSLatitudeRef = strtolower(trim($info['GPSLatitudeRef']));
$GPSLongitudeRef = strtolower(trim($info['GPSLongitudeRef']));
$lat_degrees_a = explode('/',$info['GPSLatitude'][0]);
$lat_minutes_a = explode('/',$info['GPSLatitude'][1]);
$lat_seconds_a = explode('/',$info['GPSLatitude'][2]);
$lng_degrees_a = explode('/',$info['GPSLongitude'][0]);
$lng_minutes_a = explode('/',$info['GPSLongitude'][1]);
$lng_seconds_a = explode('/',$info['GPSLongitude'][2]);
$lat_degrees = $lat_degrees_a[0] / $lat_degrees_a[1];
$lat_minutes = $lat_minutes_a[0] / $lat_minutes_a[1];
$lat_seconds = $lat_seconds_a[0] / $lat_seconds_a[1];
$lng_degrees = $lng_degrees_a[0] / $lng_degrees_a[1];
$lng_minutes = $lng_minutes_a[0] / $lng_minutes_a[1];
$lng_seconds = $lng_seconds_a[0] / $lng_seconds_a[1];
$lat = (float) $lat_degrees+((($lat_minutes*60)+($lat_seconds))/3600);
$lng = (float) $lng_degrees+((($lng_minutes*60)+($lng_seconds))/3600);
//If the latitude is South, make it negative.
//If the longitude is west, make it negative
$GPSLatitudeRef == 's' ? $lat *= -1 : '';
$GPSLongitudeRef == 'w' ? $lng *= -1 : '';
return array(
'lat' => $lat,
'lng' => $lng
);
}
}
return false;
}
Hope it helps someone.
Call this function with filename. I testet it and it works prefectly.
Call example:
$fileName='xxxxxx'; //or $fileName='xxxxxxxxx';
echo $returned_data = triphoto_getGPS($fileName);
Function:
function triphoto_getGPS($fileName)
{
//get the EXIF all metadata from Images
$exif = exif_read_data($fileName);
if(isset($exif["GPSLatitudeRef"])) {
$LatM = 1; $LongM = 1;
if($exif["GPSLatitudeRef"] == 'S') {
$LatM = -1;
}
if($exif["GPSLongitudeRef"] == 'W') {
$LongM = -1;
}
//get the GPS data
$gps['LatDegree']=$exif["GPSLatitude"][0];
$gps['LatMinute']=$exif["GPSLatitude"][1];
$gps['LatgSeconds']=$exif["GPSLatitude"][2];
$gps['LongDegree']=$exif["GPSLongitude"][0];
$gps['LongMinute']=$exif["GPSLongitude"][1];
$gps['LongSeconds']=$exif["GPSLongitude"][2];
//convert strings to numbers
foreach($gps as $key => $value){
$pos = strpos($value, '/');
if($pos !== false){
$temp = explode('/',$value);
$gps[$key] = $temp[0] / $temp[1];
}
}
//calculate the decimal degree
$result['latitude'] = $LatM * ($gps['LatDegree'] + ($gps['LatMinute'] / 60) + ($gps['LatgSeconds'] / 3600));
$result['longitude'] = $LongM * ($gps['LongDegree'] + ($gps['LongMinute'] / 60) + ($gps['LongSeconds'] / 3600));
$result['datetime'] = $exif["DateTime"];
return $result;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With