在php中从图像中读取地理标记数据

ing*_*.am 11 php geolocation

有谁知道是否有办法从PHP中的照片中读取地理标记数据?

谢谢

Wil*_*lin 23

就像其他人说的那样,exif_read_data(); 会做的.要继续获取所有数据,请使用以下参数:

exif_read_data($img, 0, true); // where $img is the path to your image
Run Code Online (Sandbox Code Playgroud)

这个函数只能读取tiffs和jpegs中的标题,我很确定只有jpegs可能包含地理标记.我已经编写了一个简单的php脚本,可以在命令行中使用,并将其作为githubgist发布.

像这样运行脚本:php exif.php

它会回显一个数组.在这里寻找坐标:

[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
Run Code Online (Sandbox Code Playgroud)

纬度和经度数组包含三个值:0表示度,1表示分钟,2表示秒.如果您看到类似"1589/100"的内容,则等于15.89.因此对于GPSLongitude数组,3609/100等于36.09.

将坐标从度 - 分 - 秒形式转换为十进制形式http://www.satsig.net/degrees-minutes-seconds-calculator.htm

如果纬度是南方,不要忘记使其为负.如果经度是西方,那就是负面的.上述数据中的坐标为:30.26483,-87.6015


Lon*_*LFs 21

考克林的回答是正确的,虽然我制定了一个快速参考的功能,以防万一有人偶然发现同样的问题.

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助某人.


svo*_*oop 1

您可以使用PHP 的EXIF 函数:

exif_read_data($file);
Run Code Online (Sandbox Code Playgroud)