开发者

Reading Geotag data from image in php

D开发者_如何学JAVAoes anyone know if there is a way to read geotag data from photos in PHP?

Thanks


Like everyone else has said, exif_read_data(); will do it. To go ahead and get all of the data, use these args:

exif_read_data($img, 0, true); // where $img is the path to your image

This function can only read headers from tiffs and jpegs and I'm pretty sure only jpegs may contain geotags. I've written a simple php script for use in the command line and posted it as a gist on github.

Run the script like this: php exif.php

It will echo out an array. Look for the coordinates here:

[GPS] => Array
    [GPSLatitudeRef] => N
    [GPSLatitude] => Array
        [0] => 30/1
        [1] => 1589/100
        [2] => 0/1
    [GPSLongitudeRef] => W
    [GPSLongitude] => Array
        [0] => 87/1
        [1] => 3609/100
        [2] => 0/1
    [GPSAltitudeRef] => 
    [GPSAltitude] => 18289/454
    [GPSTimeStamp] => Array
    [0] => 20/1
    [1] => 22/1
    [2] => 2065/1
    [GPSImgDirectionRef] => T
    [GPSImgDirection] => 34765/689

The Latitude and Longitude arrays contain three values: 0 is for degrees, 1 is for minutes and 2 is for seconds. If you see something like "1589/100" this is equal to 15.89. So for the GPSLongitude array, 3609/100 is equal to 36.09.

Convert the coordinates from degrees-minutes-second form to decimal form here http://www.satsig.net/degrees-minutes-seconds-calculator.htm

If the latitude is South, dont forget to make it negative. If the longitude is west, make that negative. The coodinates from the above data are: 30.26483, -87.6015


Will Coughlin's answer is correct though I formulated a function for quick reference in case someone stumbles upon the same problem.

/**
 * Returns an array of latitude and longitude from the Image file
 * @param image $file
 * @return multitype:number |boolean
 */
function read_gps_location($file){
    if (is_file($file)) {
        $info = exif_read_data($file);
        if (isset($info['GPSLatitude']) && isset($info['GPSLongitude']) &&
            isset($info['GPSLatitudeRef']) && isset($info['GPSLongitudeRef']) &&
            in_array($info['GPSLatitudeRef'], array('E','W','N','S')) && in_array($info['GPSLongitudeRef'], array('E','W','N','S'))) {

            $GPSLatitudeRef  = strtolower(trim($info['GPSLatitudeRef']));
            $GPSLongitudeRef = strtolower(trim($info['GPSLongitudeRef']));

            $lat_degrees_a = explode('/',$info['GPSLatitude'][0]);
            $lat_minutes_a = explode('/',$info['GPSLatitude'][1]);
            $lat_seconds_a = explode('/',$info['GPSLatitude'][2]);
            $lng_degrees_a = explode('/',$info['GPSLongitude'][0]);
            $lng_minutes_a = explode('/',$info['GPSLongitude'][1]);
            $lng_seconds_a = explode('/',$info['GPSLongitude'][2]);

            $lat_degrees = $lat_degrees_a[0] / $lat_degrees_a[1];
            $lat_minutes = $lat_minutes_a[0] / $lat_minutes_a[1];
            $lat_seconds = $lat_seconds_a[0] / $lat_seconds_a[1];
            $lng_degrees = $lng_degrees_a[0] / $lng_degrees_a[1];
            $lng_minutes = $lng_minutes_a[0] / $lng_minutes_a[1];
            $lng_seconds = $lng_seconds_a[0] / $lng_seconds_a[1];

            $lat = (float) $lat_degrees+((($lat_minutes*60)+($lat_seconds))/3600);
            $lng = (float) $lng_degrees+((($lng_minutes*60)+($lng_seconds))/3600);

            //If the latitude is South, make it negative. 
            //If the longitude is west, make it negative
            $GPSLatitudeRef  == 's' ? $lat *= -1 : '';
            $GPSLongitudeRef == 'w' ? $lng *= -1 : '';

            return array(
                'lat' => $lat,
                'lng' => $lng
            );
        }           
    }
    return false;
}

Hope it helps someone.


Call this function with filename. I testet it and it works prefectly.

Call example:

$fileName='xxxxxx'; //or $fileName='xxxxxxxxx';
echo $returned_data = triphoto_getGPS($fileName);

Function:

function triphoto_getGPS($fileName)
{
    //get the EXIF all metadata from Images
    $exif = exif_read_data($fileName);
    if(isset($exif["GPSLatitudeRef"])) {
        $LatM = 1; $LongM = 1;
        if($exif["GPSLatitudeRef"] == 'S') {
            $LatM = -1;
        }
        if($exif["GPSLongitudeRef"] == 'W') {
            $LongM = -1;
        }

        //get the GPS data
        $gps['LatDegree']=$exif["GPSLatitude"][0];
        $gps['LatMinute']=$exif["GPSLatitude"][1];
        $gps['LatgSeconds']=$exif["GPSLatitude"][2];
        $gps['LongDegree']=$exif["GPSLongitude"][0];
        $gps['LongMinute']=$exif["GPSLongitude"][1];
        $gps['LongSeconds']=$exif["GPSLongitude"][2];

        //convert strings to numbers
        foreach($gps as $key => $value){
            $pos = strpos($value, '/');
            if($pos !== false){
                $temp = explode('/',$value);
                $gps[$key] = $temp[0] / $temp[1];
            }
        }

        //calculate the decimal degree
        $result['latitude']  = $LatM * ($gps['LatDegree'] + ($gps['LatMinute'] / 60) + ($gps['LatgSeconds'] / 3600));
        $result['longitude'] = $LongM * ($gps['LongDegree'] + ($gps['LongMinute'] / 60) + ($gps['LongSeconds'] / 3600));
        $result['datetime']  = $exif["DateTime"];

        return $result;
    }
}


You can use the EXIF functions of PHP:

exif_read_data($file);


You can use the exif_read_data() function if the geotag data is embedded in the EXIF data.


Install Intervention\Image by following command.

Reference: http://image.intervention.io/getting_started/installation

composer require intervention/image

Update config/app.php

'providers' => [ Intervention\Image\ImageServiceProvider::class ], 'aliases' => [ 'Image' => Intervention\Image\Facades\Image::class ]

Use Library:

$data = Image::make(public_path('IMG.jpg'))->exif(); if(isset($data['GPSLatitude'])) { $lat = eval('return ' . $data['GPSLatitude'][0] . ';') + (eval('return ' . $data['GPSLatitude'][1] . ';') / 60) + (eval('return ' . $data['GPSLatitude'][2] . ';') / 3600); $lng = eval('return ' . $data['GPSLongitude'][0] . ';') + (eval('return ' . $data['GPSLongitude'][1] . ';') / 60) + (eval('return ' . $data['GPSLongitude'][2] . ';') / 3600); echo "$lat, $lng"; } else { echo "No GPS Info"; }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜