【问题标题】:Reading Geotag data from image in php从 php 中的图像读取地理标记数据
【发布时间】:2011-03-27 12:46:29
【问题描述】:

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

谢谢

【问题讨论】:

    标签: php geolocation


    【解决方案1】:

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

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

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

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

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

    如果纬度是南,别忘了把它设为负。如果经度为西,则设为负值。以上数据的坐标为:30.26483,-87.6015

    【讨论】:

    • 我对这个问题可能有一个基本的补充。你如何让php“做”那个数学?这使得 3609/100 等于 36.09
    • git hub 链接给了我一个 404
    【解决方案2】:

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

    希望对某人有所帮助。

    【讨论】:

      【解决方案3】:

      用文件名调用这个函数。我对它进行了测试,它运行良好。

      调用示例:

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

      【讨论】:

      • 完美工作......这个函数返回所有可能的纬度,纬度和日期时间在什么时间拍摄照片......注意。*位置应该是自己的,同时捕捉图像..in这种情况下只会显示纬度和经度,否则不会显示......如果有任何问题问......或者让我知道......我会帮忙......
      【解决方案4】:

      可以使用PHP的EXIF functions

      exif_read_data($file);
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          通过以下命令安装Intervention\Image

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

          composer require intervention/image

          更新config/app.php

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

          使用库:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-12-17
            • 2012-06-18
            • 1970-01-01
            • 1970-01-01
            • 2015-11-23
            • 1970-01-01
            • 2012-06-06
            • 2020-03-12
            相关资源
            最近更新 更多