使用PHP Version 5.2.9为exif 2.3提取exif数据的问题

时间:2012-04-09 14:53:44

标签: php gps exif

PHP Version 5.2.9

我想知道是否有人遇到问题(并且可能找到解决方案),使用PHP exif_read_data()为EXIF 2.3提取GPS甚至只是所有EXIF数据。我公司最近买了一台Fujifilm Finepix XP150,可以获取GPS方向数据,这对于我为公司建造的工具来说非常重要。

以下代码是我用来提取的代码,我通过一次获取整个EXIF数据的列表,第二次给我经度和纬度。

$exif = exif_read_data('./images/DSCF0006.JPG', 'GPS');
echo $exif===false ? "<strong>No header data found.</strong><br />\n" : "<strong>Image contains headers</strong><br />\n";

$exif = exif_read_data('./images/DSCF0006.JPG', 0, true);
echo "<strong>IMG_20120329_104351.jpg:</strong><br />\n";
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        echo "$key.$name: $val<br />\n";
    }
}
$dir = "./images/";               

function readGPSinfoEXIF($image_full_name) {
    $exif=exif_read_data($image_full_name, 0, true);
    if(!$exif || $exif['GPS']['GPSLatitude'] == '') {
        return false;
    } else {
        $lat_ref = $exif['GPS']['GPSLatitudeRef'];
        echo "Lattitude Reference: ", $lat_ref, "<br />";
        $lat = $exif['GPS']['GPSLatitude'];
        list($num, $dec) = explode('/', $lat[0]);
        $lat_s = $num / $dec;
        list($num, $dec) = explode('/', $lat[1]);
        $lat_m = $num / $dec;
        list($num, $dec) = explode('/', $lat[2]);
        $lat_v = $num / $dec;
        $lon_ref = $exif['GPS']['GPSLongitudeRef'];
        echo "Longitude Reference: ", $lon_ref, "<br />";
        $lon = $exif['GPS']['GPSLongitude'];
        list($num, $dec) = explode('/', $lon[0]);
        $lon_s = $num / $dec;
        list($num, $dec) = explode('/', $lon[1]);
        $lon_m = $num / $dec;
        list($num, $dec) = explode('/', $lon[2]);
        $lon_v = $num / $dec;

        $lat_int = ($lat_s + $lat_m / 60.0 + $lat_v / 3600.0);
        // check orientation of latitude and prefix with (-) if S
        $lat_int = ($lat_ref == "S") ? '-' . $lat_int : $lat_int;

        $lon_int = ($lon_s + $lon_m / 60.0 + $lon_v / 3600.0);
        // check orientation of longitude and prefix with (-) if W
        $lon_int = ($lon_ref == "W") ? '-' . $lon_int : $lon_int;

        $gps_int = array($lat_int, $lon_int);
        return $gps_int;
        }
}


function dirImages($dir) {
    $d = dir($dir); 
    while (false!== ($file = $d->read())) {
        $extension = substr($file, strrpos($file, '.')); 
        if($extension == ".JPG" || $extension == ".jpeg" || $extension == ".gif" | $extension == ".png") {
            $images[$file] = $file; 
        }
        $d->close();         
        return $images;
    }         

    $array = dirImages('./images/');
    $counter = 0;

    foreach ($array as $key => $image) {
        echo "<br />";
        $counter++;
        echo "<strong>".$counter."</strong>";
        echo ":  ";
        $image_full_name = $dir."/".$key;
        $image_name = $key;
        echo "<strong>".$image_name."</strong>";
        echo "<br />";
        $results = readGPSinfoEXIF($image_full_name);
        $latitude = $results[0];
        echo $latitude;
        echo ", ";
        $longitude = $results[1];
        echo $longitude;
        echo "<br />";
    }

使用我的三星Galaxy Nexus拍摄照片时,我获得了完美的效果,我获得了相应的GPS数据和所有EXIF数据,在使用相机时我得到exif_read_data(DSCF0006.JPG) [exif_read_data]: corrupt EXIF header: maximum directory nesting level reached

我做错了还是PHP 5 exif_read_data不支持EXIF 2.3?我研究了这个,PHP.net声明:

exif_read_data() also validates EXIF data tags according to the EXIF
specification (» http://exif.org/Exif2-2.PDF, page 20).

1 个答案:

答案 0 :(得分:2)

我也有一台Fujifilm相机有同样的问题,但我想我找到了解决方案,我在这里提出了一个PHP错误报告: https://bugs.php.net/bug.php?id=66443

如果可以从源代码编译PHP,请在exif.c文件中增加MAX_IFD_NESTING_LEVEL的值。我把它从100提高到200,似乎解决了这个问题。