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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5449282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:27:58  来源:igfitidea点击:

Reading Geotag data from image in php

phpgeolocation

提问by ingh.am

Does anyone know if there is a way to read geotag data from photos in PHP?

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

Thanks

谢谢

回答by Will Coughlin

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(); 会做的。要继续获取所有数据,请使用以下参数:

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.

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

Run the script like this: php exif.php

像这样运行脚本: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.

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

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

在此处将坐标从度-分-秒形式转换为十进制形式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

如果纬度是南,不要忘记使它为负。如果经度为西,则将其设为负值。上述数据的坐标为:30.26483,-87.6015

回答by LoneWOLFs

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

Will Coughlin的回答是正确的,尽管我制定了一个功能以供快速参考,以防有人偶然发现同样的问题。

/**
 * 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.

希望它可以帮助某人。

回答by Raghwendra Pathak

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

回答by svoop

You can use the EXIF functionsof PHP:

您可以使用PHP的EXIF 函数

exif_read_data($file);

回答by Hymanbot

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

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

回答by Web Developer in Pune

Install Intervention\Imageby following command.

Intervention\Image按照以下命令安装。

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

参考:http: //image.intervention.io/getting_started/installation

composer require intervention/image

composer require intervention/image

Update config/app.php

更新 config/app.php

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

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

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