Skip to content

Commit c3185ba

Browse files
author
Marek Szymczuk
committed
Fixed normalization of "ExposureTime" (prevent division by zero)
1 parent d10ee92 commit c3185ba

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

lib/PHPExif/Mapper/Exiftool.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,14 @@ public function mapRawData(array $data)
143143
}
144144
break;
145145
case self::EXPOSURETIME:
146-
$value = '1/' . round(1 / $value);
146+
// Based on the source code of Exiftool (PrintExposureTime subroutine):
147+
// http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-9.90/lib/Image/ExifTool/Exif.pm
148+
if ($value < 0.25001 && $value > 0) {
149+
$value = sprintf('1/%d', intval(0.5 + 1 / $value));
150+
} else {
151+
$value = sprintf('%.1f', $value);
152+
$value = preg_replace('/.0$/', '', $value);
153+
}
147154
break;
148155
case self::FOCALLENGTH:
149156
$focalLengthParts = explode(' ', $value);

lib/PHPExif/Mapper/Native.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,18 @@ public function mapRawData(array $data)
150150
}
151151
break;
152152
case self::EXPOSURETIME:
153-
// normalize ExposureTime
154-
// on one test image, it reported "10/300" instead of "1/30"
155-
list($counter, $denominator) = explode('/', $value);
156-
if (intval($counter) !== 1) {
157-
$denominator /= $counter;
153+
if (!is_float($value)) {
154+
$value = $this->normalizeComponent($value);
155+
}
156+
157+
// Based on the source code of Exiftool (PrintExposureTime subroutine):
158+
// http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-9.90/lib/Image/ExifTool/Exif.pm
159+
if ($value < 0.25001 && $value > 0) {
160+
$value = sprintf('1/%d', intval(0.5 + 1 / $value));
161+
} else {
162+
$value = sprintf('%.1f', $value);
163+
$value = preg_replace('/.0$/', '', $value);
158164
}
159-
$value = '1/' . round($denominator);
160165
break;
161166
case self::FOCALLENGTH:
162167
$parts = explode('/', $value);
@@ -217,7 +222,7 @@ protected function isSection($field)
217222
*/
218223
protected function extractGPSCoordinate(array $components)
219224
{
220-
$components = array_map(array($this, 'normalizeGPSComponent'), $components);
225+
$components = array_map(array($this, 'normalizeComponent'), $components);
221226

222227
if (count($components) > 2) {
223228
return intval($components[0]) + (intval($components[1]) / 60) + (floatval($components[2]) / 3600);
@@ -227,12 +232,12 @@ protected function extractGPSCoordinate(array $components)
227232
}
228233

229234
/**
230-
* Normalize GPS coordinates components
235+
* Normalize component
231236
*
232237
* @param mixed $component
233238
* @return int|float
234239
*/
235-
protected function normalizeGPSComponent($component)
240+
protected function normalizeComponent($component)
236241
{
237242
$parts = explode('/', $component);
238243

0 commit comments

Comments
 (0)