How to Get Latitude and Longitude from Image in PHP

Geographical location can be tagged with the photograph by geotagging. Generally, the latitude and longitude are assigned to the image to geotagged photograph. Geotagging is done by two ways, automatic or manual. If geolocation is enabled, the geotagging is automatically done by the device. Alternatively, you can tag geodata to the photo manually.

In this tutorial, we will show you how to retrieve geolocation information from the image in PHP. There is an easy way to get Geotag data from the image using PHP. You can get the location where picture was taken using PHP.

In our example script, the latitude and longitude will be retrieved from the image in PHP. This latitude and longitude can be used to show the location of the image on Google map.

Get Latitude and Longitude from Image

The exif_read_data() function in PHP, reads the EXIF headers from an image file. We will use exif_read_data() function to get the GPS data from image. For better usability, we’ll group all code together in a custom PHP function called get_image_location(). The get_image_location() function returns an array of latitude and longitude from the image file.

 * get_image_location
 * Returns an array of latitude and longitude from the Image file
 * @param $image file path
 * @return multitype:array|boolean
function get_image_location($image ''){
$exif exif_read_data($image0true);
$exif && isset($exif['GPS'])){
$GPSLatitudeRef $exif['GPS']['GPSLatitudeRef'];
$GPSLatitude    $exif['GPS']['GPSLatitude'];
$GPSLongitude   $exif['GPS']['GPSLongitude'];
$lat_degrees count($GPSLatitude) > gps2Num($GPSLatitude[0]) : 0;
$lat_minutes count($GPSLatitude) > gps2Num($GPSLatitude[1]) : 0;
$lat_seconds count($GPSLatitude) > gps2Num($GPSLatitude[2]) : 0;
$lon_degrees count($GPSLongitude) > gps2Num($GPSLongitude[0]) : 0;
$lon_minutes count($GPSLongitude) > gps2Num($GPSLongitude[1]) : 0;
$lon_seconds count($GPSLongitude) > gps2Num($GPSLongitude[2]) : 0;
$lat_direction = ($GPSLatitudeRef == 'W' or $GPSLatitudeRef == 'S') ? -1;
$lon_direction = ($GPSLongitudeRef == 'W' or $GPSLongitudeRef == 'S') ? -1;
$latitude $lat_direction * ($lat_degrees + ($lat_minutes 60) + ($lat_seconds / (60*60)));
$longitude $lon_direction * ($lon_degrees + ($lon_minutes 60) + ($lon_seconds / (60*60)));

        return array(

The gps2Num() is a helper function, used in get_image_location() function to convert GPS coord part in float val.

function gps2Num($coordPart){
$parts explode('/'$coordPart);
count($parts) <= 0)
count($parts) == 1)
floatval($parts[0]) / floatval($parts[1]);

Pass an image file path in get_image_location() function to get the latitude and longitude from the image.

//image file path
$imageURL "images/geotagged-image.jpeg";

//get location of image
$imgLocation get_image_location($imageURL);

//latitude & longitude
$imgLat $imgLocation['latitude'];
$imgLng $imgLocation['longitude'];

Display Location on Google Map where Picture was Taken

If you want to show the image geolocation on Google Map, it can be done easily by Google Maps JavaScript API. Use the following script to mark image location on Google Map using Google Maps JavaScript API.

Google Maps JavaScript API:
At first, you need to include the Google Maps JavaScript API and specify this API key in API URL.

<script type="text/javascript" src=""></script>

Don’t have an API key, see this step-by-step guide to get your own API key – How to Get Google Maps JavaScript API Key

JavaScript Code:
Specify the latitude and longitude from GPS data of the image file.

var myCenter = new google.maps.LatLng(<?php echo $imgLat?>, <?php echo $imgLng?>);
function initialize(){
    var mapProp = {

    var map = new google.maps.Map(document.getElementById("map"),mapProp);

    var marker = new google.maps.Marker({

google.maps.event.addDomListener(window, 'load', initialize);

HTML Code:
Define an area to display Google Map.

<div id="map"></div>

CSS Code
Use CSS to set the size of the map div.

    width: 100%;
    height: 400px;

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request

Recommended Tutorials For You

Leave a reply