PHP 版本 5.2.9
我想知道是否有人在提取 GPS 或什至只是使用 PHPexif_read_data()
用于 EXIF 2.3 的所有 EXIF 数据时遇到问题(并且可能找到了解决方案)。我的公司最近购买了一台允许 GPS 定向数据的 Fujifilm Finepix XP150,这对于我正在为我的公司构建的工具至关重要。
下面的代码是我用来提取的,我通过一次以获得整个 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 5exif_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).