Search code examples
phpgpslatitude-longitude

checking if exif latitude & longitude exists in image


So i'm playing around with with the read_exif_data function, and came across a little problem. I have to stop the fileupload if the image dosen't have a longitude & latitude in it.

problem is that I can't find a way to check if the gps array is in the exif data.

I'm reading the data from $ _ FILES['spotImg']['tmp_name'] And I do get the exif that I nedd.

The problem is only when there is no gps data in the image.

if($_FILES['spotImg']['error'] == 0){

            $file = "image_".uniqid(). $_FILES['spotImg']['name'];
            $img = WideImage::loadFromFile($_FILES['spotImg']['tmp_name']);

            //check if exif data is avalible
            $temp = $_FILES['spotImg']['tmp_name'];
            $exif = read_exif_data($temp, 0, true);

            echo "<pre">;
            print_r($exif);
            echo "</pre>;

}

This is what I was thinking, but it dosen't work work

if(isset($exif['GPSLatitude'])){
     //run code if long/latitude was found
   }else{
    //give a error message if long/latitude was NOT found
}

this is the array I wanna check if exist

[GPS] => Array
    (
        [GPSLatitudeRef] => N
        [GPSLatitude] => Array
            (
                [0] => 55/1
                [1] => 41/1
                [2] => 2846/100
            )

        [GPSLongitudeRef] => E
        [GPSLongitude] => Array
            (
                [0] => 12/1
                [1] => 33/1
                [2] => 1568/100
            )

        [GPSAltitudeRef] => 
        [GPSAltitude] => 68780/4027
        [GPSTimeStamp] => Array
            (
                [0] => 12/1
                [1] => 48/1
                [2] => 4621/100
            )

        [GPSSpeedRef] => K
        [GPSSpeed] => 0/1
        [GPSDateStamp] => 2015:09:16
    )

Solution

  • I had a similar issue with reading the ['orientation'] property of an image; some don't have any.

    The solution I found was to do a check with !empty() as follows:

    if(!empty($exif['GPSLatitude'])){
      //run code if long/latitude was found
    }else{
      //give an error message if long/latitude was NOT found
    }
    

    This check should work without throwing any errors.