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 geotagged images. Geotagging is done in 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.

If geolocation is tagged with a photo, the GPS data can be retrieved from it in the future. There is an easy way to get Geotag data from the image using PHP. You can get the location where the picture was taken using PHP. In this tutorial, we will show you how to retrieve geolocation information from the image in PHP.

In this example script, we will retrieve the GPS data (latitude and longitude) 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, helps to read 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 latitude and longitude from the image file in an array format.
/**
 * 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);
    if(
$exif && isset($exif['GPS'])){
        
$GPSLatitudeRef $exif['GPS']['GPSLatitudeRef'];
        
$GPSLatitude    $exif['GPS']['GPSLatitude'];
        
$GPSLongitudeRef$exif['GPS']['GPSLongitudeRef'];
        
$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(
'latitude'=>$latitude'longitude'=>$longitude);
    }else{
        return 
false;
    }
}

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);
    if(
count($parts) <= 0)
    return 
0;
    if(
count($parts) == 1)
    return 
$parts[0];
    return 
floatval($parts[0]) / floatval($parts[1]);
}

Usage:
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 code to mark photo location on Map using Google Maps JavaScript API.

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

<script src="https://maps.googleapis.com/maps/api/js?key=Your_API_KEY"></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 the GPS data of the image file.

<script>
var myCenter = new google.maps.LatLng(<?php echo $imgLat?>, <?php echo $imgLng?>);
function initialize(){
    var mapProp = {
        center:myCenter,
        zoom:10,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };

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

    var marker = new google.maps.Marker({
        position:myCenter,
        animation:google.maps.Animation.BOUNCE
    });

    marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>

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.

#map{
    width: 100%;
    height: 400px;
}

Conclusion

This example script is very helpful to get geolocation from photo and mark on Google Map. You can retrieve the picture taken location from the image dynamically using PHP. The user can find the geolocation of the image easily with PHP.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up