PHP extract GPS EXIF data

前端 未结 13 1428
执笔经年
执笔经年 2020-11-29 16:26

I would like to extract the GPS EXIF tag from pictures using php. I\'m using the exif_read_data() that returns a array of all tags + data :

GPS.         


        
13条回答
  •  庸人自扰
    2020-11-29 16:52

    This is an old question but felt it could use a more eloquent solution (OOP approach and lambda to process the fractional parts)

    /**
     * Example coordinate values
     *
     * Latitude - 49/1, 4/1, 2881/100, N
     * Longitude - 121/1, 58/1, 4768/100, W
     */
    protected function _toDecimal($deg, $min, $sec, $ref) {
    
        $float = function($v) {
            return (count($v = explode('/', $v)) > 1) ? $v[0] / $v[1] : $v[0];
        };
    
        $d = $float($deg) + (($float($min) / 60) + ($float($sec) / 3600));
        return ($ref == 'S' || $ref == 'W') ? $d *= -1 : $d;
    }
    
    
    public function getCoordinates() {
    
        $exif = @exif_read_data('image_with_exif_data.jpeg');
    
        $coord = (isset($exif['GPSLatitude'], $exif['GPSLongitude'])) ? implode(',', array(
            'latitude' => sprintf('%.6f', $this->_toDecimal($exif['GPSLatitude'][0], $exif['GPSLatitude'][1], $exif['GPSLatitude'][2], $exif['GPSLatitudeRef'])),
            'longitude' => sprintf('%.6f', $this->_toDecimal($exif['GPSLongitude'][0], $exif['GPSLongitude'][1], $exif['GPSLongitude'][2], $exif['GPSLongitudeRef']))
        )) : null;
    
    }
    

提交回复
热议问题