无法正确访问exif数组中的值

时间:2016-12-16 22:34:18

标签: php gps

我正在尝试从exif获取gps lat和log,但我无法正确访问索引。 这是基本的var_dump的样子:

array(7) { ["FILE"]=> array(6) { ["FileName"]=> string(12) "DSC00043.JPG" ["FileDateTime"]=> int(1481834862) ["FileSize"]=> int(4259986) ["FileType"]=> int(2) ["MimeType"]=> string(10) "image/jpeg" ["SectionsFound"]=> string(44) "ANY_TAG, IFD0, THUMBNAIL, EXIF, GPS, INTEROP" } ["COMPUTED"]=> array(10) { ["html"]=> string(26) "width="6000" height="4000"" ["Height"]=> int(4000) ["Width"]=> int(6000) ["IsColor"]=> int(1) ["ByteOrderMotorola"]=> int(0) ["ApertureFNumber"]=> string(5) "f/5.6" ["UserComment"]=> NULL ["UserCommentEncoding"]=> string(5) "ASCII" ["Thumbnail.FileType"]=> int(2) ["Thumbnail.MimeType"]=> string(10) "image/jpeg" } ["IFD0"]=> array(13) { ["ImageDescription"]=> string(31) " " ["Make"]=> string(4) "SONY" ["Model"]=> string(9) "ILCE-6300" ["Orientation"]=> int(1) ["XResolution"]=> string(5) "350/1" ["YResolution"]=> string(5) "350/1" ["ResolutionUnit"]=> int(2) ["Software"]=> string(15) "ILCE-6300 v1.10" ["DateTime"]=> string(19) "2016:01:17 22:26:22" ["YCbCrPositioning"]=> int(2) ["Exif_IFD_Pointer"]=> int(8) ["GPS_IFD_Pointer"]=> int(47147) ["UndefinedTag:0xC4A5"]=> string(106) "PrintIM0300 " ' '� '� ' '^ '� '� '� '" } ["THUMBNAIL"]=> array(13) { ["Compression"]=> int(6) ["ImageDescription"]=> string(31) " " ["Make"]=> string(4) "SONY" ["Model"]=> string(9) "ILCE-6300" ["Orientation"]=> int(1) ["XResolution"]=> string(4) "72/1" ["YResolution"]=> string(4) "72/1" ["ResolutionUnit"]=> int(2) ["Software"]=> string(15) "ILCE-6300 v1.10" ["DateTime"]=> string(19) "2016:01:17 22:26:22" ["JPEGInterchangeFormat"]=> int(38350) ["JPEGInterchangeFormatLength"]=> int(8797) ["YCbCrPositioning"]=> int(2) } ["EXIF"]=> array(38) { ["ExposureTime"]=> string(6) "1/1000" ["FNumber"]=> string(5) "56/10" ["ExposureProgram"]=> int(1) ["ISOSpeedRatings"]=> int(1000) ["UndefinedTag:0x8830"]=> int(2) ["UndefinedTag:0x8832"]=> int(1000) ["ExifVersion"]=> string(4) "0230" ["DateTimeOriginal"]=> string(19) "2016:01:17 22:26:22" ["DateTimeDigitized"]=> string(19) "2016:01:17 22:26:22" ["ComponentsConfiguration"]=> string(4) " " ["CompressedBitsPerPixel"]=> string(3) "1/1" ["BrightnessValue"]=> string(10) "21294/2560" ["ExposureBiasValue"]=> string(4) "0/10" ["MaxApertureValue"]=> string(7) "760/256" ["MeteringMode"]=> int(5) ["LightSource"]=> int(0) ["Flash"]=> int(16) ["FocalLength"]=> string(6) "190/10" ["MakerNote"]=> string(9) "SONY DSC " ["UserComment"]=> string(64) "ASCII" ["FlashPixVersion"]=> string(4) "0100" ["ColorSpace"]=> int(1) ["ExifImageWidth"]=> int(6000) ["ExifImageLength"]=> int(4000) ["InteroperabilityOffset"]=> int(47385) ["FileSource"]=> string(1) " " ["SceneType"]=> string(1) " " ["CustomRendered"]=> int(0) ["ExposureMode"]=> int(1) ["WhiteBalance"]=> int(0) ["DigitalZoomRatio"]=> string(5) "16/16" ["FocalLengthIn35mmFilm"]=> int(28) ["SceneCaptureType"]=> int(0) ["Contrast"]=> int(0) ["Saturation"]=> int(0) ["Sharpness"]=> int(0) ["UndefinedTag:0xA432"]=> array(4) { [0]=> string(6) "190/10" [1]=> string(6) "190/10" [2]=> string(5) "28/10" [3]=> string(5) "28/10" } ["UndefinedTag:0xA434"]=> string(11) "E 19mm F2.8" } ["GPS"]=> array(6) { ["GPSVersion"]=> string(4) " " ["GPSLatitudeRef"]=> string(1) "N" ["GPSLatitude"]=> array(3) { [0]=> string(4) "34/1" [1]=> string(3) "5/1" [2]=> string(11) "231365/9853" } ["GPSLongitudeRef"]=> string(1) "W" ["GPSLongitude"]=> array(3) { [0]=> string(4) "83/1" [1]=> string(4) "59/1" [2]=> string(11) "100499/4849" } ["GPSAltitude"]=> string(12) "2219798/6091" } ["INTEROP"]=> array(2) { ["InterOperabilityIndex"]=> string(3) "R98" ["InterOperabilityVersion"]=> string(4) "0100" } }

我一直在使用这个函数来获取我的$exifs['FileName'];,因为如果我只是回应它,它也是一个未知的索引:我也尝试在这个函数中访问gps数组。

public function get_data($files){
    $data = array();
    $name = '';
    foreach ($files as $file) {
        $exifs   = exif_read_data($file, 0, true);
        //var_dump($exifs);
        foreach ($exifs as $exif) {
            extract($exif);
            $name = $FileName;
            foreach ($GPS as $key) {
                echo $key;
                # code...
            }
            break;
        }
    }
    return $name;
}

1 个答案:

答案 0 :(得分:0)

您可以通过指定键来简单地从这样的对象访问值: 例如$exifs['FILE']['FileName']

要获取所有文件的文件名,lat和lon值,您可以执行以下操作:

public function get_data($files) {
    $data = array();
    foreach ($files as $file) {
        $exifs = exif_read_data($file, 0, true);
        $data[] = [
            'filename' => $exifs['FILE']['FileName'],
            'gps_lat' => $exifs['GPS']['GPSLatitude'],
            'gps_lon' => $exifs['GPS']['GPSLongitude'],
        ]
    }
    return $data;
}

值得注意的是,lat和lon值有点时髦,而不是通常的'格式。请参阅this answer将gps_lat和gps_lon值转换为浮点数。