Problems retrieving exif data for exif 2.3 using PHP version 5.2.9
PHP Version 5.2.9
I was wondering if anyone was having trouble (and possibly finding a resolution) with extracting GPS or even just all EXIF data using PHP exif_read_data()
for EXIF 2.3. My company recently bought a Fujifilm Finepix XP150 which allows directional GPS data, which is important for a tool I am building for my company.
Below is the code I am using to extract, I go through once to list all EXIF data, and second time gives me longitude and latitude.
$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 />";
}
I get great results when taking photos with my Galaxy Nexus, I get the corresponding GPS data and all EXIF data, when using the camera I get exif_read_data(DSCF0006.JPG) [exif_read_data]: corrupt EXIF header: maximum directory nesting level reached
Am I doing something wrong or PHP 5 exif_read_data
doesn't support EXIF 2.3 yet? I researched this, PHP.net states:
exif_read_data() also validates EXIF data tags according to the EXIF
specification (» http://exif.org/Exif2-2.PDF, page 20).
source to share
I also have a Fujifilm camera with the same problem, but I think I found a solution, I put together a PHP bug report here: https://bugs.php.net/bug.php?id=66443
If you can compile PHP from source, increase the MAX_IFD_NESTING_LEVEL value in exif.c. I raised it from 100 to 200 and it seems to have fixed the problem.
source to share