Modern cameras equipped with GPS receivers add the GPS coordinates to every photographs it takes. Using this data, the exact location where the photograph was taken can be determined. This information can be very useful when archiving or organizing the photographs. The GPS data will be embedded in the photo along with the EXIF header. In this post I will show you how to extract this information and convert it to some meaningful data using PHP.
PHP has a build in function exif_read_data() which reads the entire exif header and returns it as an array. So this function can be utilized to obtain the GPS data.
First of all obtain a photograph with some GPS data. You may try a Google Image search for pictures with GPS data.
The following code will extract and display the GPS data if it is present.
The following screenshot shows the expected data obtained from running the above code with an image containing GPS data.
You can notice that the [GPSLatitudeRef] has a value N (North) which is the latitude reference and the [GPSLongitudeRef] has a value W (West) which is the longitude reference.
The actual latitude and longitude values are represented as arrays at index [GPSLatitude] and [GPSLongitude] respectively. Both these arrays have three values which represents Degrees, Minutes and Seconds as rational numbers.
[0] => Degrees
[1] => Minutes
[2] => Seconds
So you have to extract each of these values and then convert the rational numbers to a fraction by performing division.
A sample code to perform this is shown below.
Similarly you can obtain the DMS (Degrees Minutes Seconds) values for Longitude also.
I guess you might be wondering how easy it is to get the GPS data from a Photograph. So go ahead and make your photo gallery more interesting by displaying the GPS data and if necessary, you may also display a map using Google Maps along with the image showing where the picture was clicked.
If you want any help in converting the GPS data from Degrees Minutes Seconds format to decimal format, please check this out : Convert Latitudes and Longitudes from Degrees Minutes Seconds format to Decimal format
PHP has a build in function exif_read_data() which reads the entire exif header and returns it as an array. So this function can be utilized to obtain the GPS data.
First of all obtain a photograph with some GPS data. You may try a Google Image search for pictures with GPS data.
The following code will extract and display the GPS data if it is present.
<?php $filename = 'picture.jpg'; $data = exif_read_data($filename, NULL, 1); if(isset($data['GPS'])) { print_r($data['GPS']); } else { echo 'No GPS data available.'; } ?>
The following screenshot shows the expected data obtained from running the above code with an image containing GPS data.
You can notice that the [GPSLatitudeRef] has a value N (North) which is the latitude reference and the [GPSLongitudeRef] has a value W (West) which is the longitude reference.
The actual latitude and longitude values are represented as arrays at index [GPSLatitude] and [GPSLongitude] respectively. Both these arrays have three values which represents Degrees, Minutes and Seconds as rational numbers.
[0] => Degrees
[1] => Minutes
[2] => Seconds
So you have to extract each of these values and then convert the rational numbers to a fraction by performing division.
A sample code to perform this is shown below.
<?php //Calculations for Latitude $degrees = $data['GPS']['GPSLatitude'][0]; $parts = explode('/', $degrees); $degrees = $parts[0] / $parts[1]; $minutes = $data['GPS']['GPSLatitude'][1]; $parts = explode('/', $minutes); $minutes = $parts[0] / $parts[1]; $seconds = $data['GPS']['GPSLatitude'][2]; $parts = explode('/', $seconds); $seconds = $parts[0] / $parts[1]; ?>
Similarly you can obtain the DMS (Degrees Minutes Seconds) values for Longitude also.
I guess you might be wondering how easy it is to get the GPS data from a Photograph. So go ahead and make your photo gallery more interesting by displaying the GPS data and if necessary, you may also display a map using Google Maps along with the image showing where the picture was clicked.
If you want any help in converting the GPS data from Degrees Minutes Seconds format to decimal format, please check this out : Convert Latitudes and Longitudes from Degrees Minutes Seconds format to Decimal format
This article is pretty good. Thanks!
ReplyDeleteIn the [GPS] array,there seems to be some altitude information as well. How to decode it?
ReplyDeleteI'm almost sure you don't need this anymore, but still:
Delete$data = exif_read_data($path_to_your_photo, 0, TRUE);
$alt = explode('/', $data["GPS"]["GPSAltitude"]);
$altitude = (isset($alt[1])) ? ($alt[0] / $alt[1]) : $alt[0];
Good article. I am going to use this in my future project.
ReplyDelete