Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 23:19, 29 July 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Extract GPS coordinates from digital camera images using PHP

From Wiki
Jump to: navigation, search
Article Metadata
Created: Maveric (04 Jun 2010)
Last edited: hamishwillee (29 Jul 2012)



The photos taken with digital cameras, mobile or otherwise, contain a lot of interesting information that is stored inside the image in a metadata format called EXIF (exchangeable image file) . This data makes each image unique. EXIF data contains information like, model of the camera, the aperture used, ISO level, and if the flash was used or not.

Amongst the information in the EXIF data will be geolocation; latitude and longitude of the place that the photo was initially shot. This information would be useful e.g. in conjunction with a mapping API to show the image on the actual geolocation over the world map. There are already online photo sharing communities e.g. Panoramio, locr or Flickr, which do allow uploading of geocoded images or to add geolocation information online.

Nokia has developed a series of mobile phones that are equipped with a GPS receiver and therefore able to tag all captured images with the location Note that on a mobile device that supports it, one has to remember to enable the GPS location feature in the Camera application setup.

The main objective of this article is to provide an example of how to extract the data in the digital camera image file, and place the data into an array, which can be then used further. The script is written in PHP and can run in a Windows / Linux environment, hosted either locally or remotely that is configured to support PHP. In the pre-requisites it is mentioned that the PHP needs to be configured to support reading the EXIF information. How to configure this, will be explained further in this wiki article. Please note that the PHP script in this Wiki article represents only basic functionality to perform the tasks.

Interaction with a WRT widget


- a Nokia mobile device with GPS support

Note: Storing GPS info must have been enabled when taking images for using the PHP script. If the images do not contain location data, the script will inform the user and exit.

- Web server/hosting with full access rights to modify and edit files at web root level - Modify the php.ini file to enable the exif dynamic link library. Please see the instructions below:

Enabling PHP EXIF support on local server, in this example we have installed WAMP package. The message indicates that the EXIF extension has not been enabled.

EXIF not enabled.png

Click open the [ Setup MySQL in WAMP - password setting, service shutdown & startup WAMPServer] menu on the right screen bottom corner and enable the php_exif parameter in the list shown on the picture below.

EXIF parameter.png

You can also directly edit the PHP.ini file and uncomment the php_exif line in it.


Note: You may need to put the following line enabled before the php_exif.dll to make this work:


PHP script (server-side)

This script will read the GPS location data in each of the images fetched in function dirImages()

$dir = "./img";
function readGPSinfoEXIF($image_full_name)
$exif=exif_read_data($image_full_name, 0, true);
if(!$exif || $exif['GPS']['GPSLatitude'] == '') {
return false;
} else {
$lat_ref = $exif['GPS']['GPSLatitudeRef'];
$lat = $exif['GPS']['GPSLatitude'];
list($num, $dec) = explode('/', $lat[0]);
$lat_s = $num / $dec;
list($num, $dec) = explode('/', $lat[1]);
$lat_m = $num / $dec;
list($num, $dec) = explode('/', $lat[2]);
$lat_v = $num / $dec;
$lon_ref = $exif['GPS']['GPSLongitudeRef'];
$lon = $exif['GPS']['GPSLongitude'];
list($num, $dec) = explode('/', $lon[0]);
$lon_s = $num / $dec;
list($num, $dec) = explode('/', $lon[1]);
$lon_m = $num / $dec;
list($num, $dec) = explode('/', $lon[2]);
$lon_v = $num / $dec;
$gps_int = array($lat_s + $lat_m / 60.0 + $lat_v / 3600.0, $lon_s
+ $lon_m / 60.0 + $lon_v / 3600.0);
return $gps_int;
function dirImages($dir)
$d = dir($dir);
while (false!== ($file = $d->read()))
$extension = substr($file, strrpos($file, '.'));
if($extension == ".jpg" || $extension == ".jpeg" || $extension == ".gif"
|$extension == ".png")
$images[$file] = $file;
return $images;
$array = dirImages('./img');
$counter = 0;
foreach ($array as $key => $image)
echo "<br />";
echo $counter;
echo "<br />";
$image_full_name = $dir."/".$key;
echo $image_full_name;
$results = readGPSinfoEXIF($image_full_name);
$latitude = $results[0];
echo $latitude;
echo "<br />";
$longitude = $results[1];
echo $longitude;
echo "<br />";

Example output

Example output for three (3) photos (latitude, longitude pairs). You may use these pairs to put e.g. a marker on the map.


Tested with

localhost & remote hosting

Development Environment

  • Camera used : Nokia N97
  • WAMP Server (localhost) running on a Windows XP Pro desktop
  • Remote web service with Windows hosting
This page was last modified on 29 July 2012, at 23:19.
345 page views in the last 30 days.