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

在php中从图像读取地理标记数据,php,geolocation,Php,Geolocation,有人知道是否有一种方法可以在PHP中从照片中读取地理标签数据吗 谢谢您可以使用PHP的: 如果地理标记数据嵌入到EXIF数据中,则可以使用该函数。正如其他人所说,EXIF_read_data();我会的。 要继续并获取所有数据,请使用以下参数: exif_read_data($img, 0, true); // where $img is the path to your image 这个函数只能从TIFF和JPEG中读取标题,我很确定只有JPEG可能包含地理标签。我已经编写了一个简单的php

有人知道是否有一种方法可以在PHP中从照片中读取地理标签数据吗

谢谢

您可以使用PHP的:


如果地理标记数据嵌入到EXIF数据中,则可以使用该函数。

正如其他人所说,EXIF_read_data();我会的。 要继续并获取所有数据,请使用以下参数:

exif_read_data($img, 0, true); // where $img is the path to your image
这个函数只能从TIFF和JPEG中读取标题,我很确定只有JPEG可能包含地理标签。我已经编写了一个简单的php脚本,在命令行中使用,并将其发布为

像这样运行脚本: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
纬度和经度数组包含三个值:0表示度,1表示分钟,2表示秒。如果你看到类似“1589/100”的东西,这等于15.89。因此,对于GPSLongitude数组,3609/100等于36.09

将坐标从第二种形式转换为十进制形式

如果纬度是南方,别忘了把它定为负值。如果经度是西,则将其设为负数。上述数据的坐标为:30.26483,-87.6015。尽管我制定了一个函数,以便在遇到相同问题时快速参考,但我的答案是正确的

/**
 * 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;
}

希望它能帮助别人。

使用文件名调用此函数。我对它进行了测试,效果非常好

呼叫示例:

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

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;
    }
}

通过以下命令安装
Intervention\Image

参考:

composer需要干预/image

更新
config/app.php

“提供者”=>[
干预\Image\ImageServiceProvider::类
],
“别名”=>[
'Image'=>Intervention\Image\Facades\Image::class
]

使用图书馆:

$data=Image::make(public_path('IMG.jpg'))->exif();
如果(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);
回声“$lat$lng”;
}否则{
回显“无GPS信息”;
}

我对这个问题可能有一个基本的补充。如何让php“做”这些数学运算?也就是说,3609/100等于36。09@LOlliffegit hub link给了我一个很好的答案……这个函数返回所有可能的纬度,LANGITE和datetime拍摄照片的时间…注意。*拍摄图像时,位置应为自己的位置。在这种情况下,仅显示纬度和经度,否则不会显示。如果有任何问题,请询问..或让我知道..我将提供帮助。。。。。
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;
    }
}