Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/PHPExif/Exif.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ public function getExposureMilliseconds()
return false;
}

if (is_numeric($this->data[self::EXPOSURE])) {
return $this->data[self::EXPOSURE] + 0;
}

$exposureParts = explode('/', $this->data[self::EXPOSURE]);

return (int) reset($exposureParts) / (int) end($exposureParts);
Expand Down
9 changes: 8 additions & 1 deletion lib/PHPExif/Mapper/Exiftool.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,14 @@ public function mapRawData(array $data)
}
break;
case self::EXPOSURETIME:
$value = '1/' . round(1 / $value);
// Based on the source code of Exiftool (PrintExposureTime subroutine):
// http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-9.90/lib/Image/ExifTool/Exif.pm
if ($value < 0.25001 && $value > 0) {
$value = sprintf('1/%d', intval(0.5 + 1 / $value));
} else {
$value = sprintf('%.1f', $value);
$value = preg_replace('/.0$/', '', $value);
}
break;
case self::FOCALLENGTH:
$focalLengthParts = explode(' ', $value);
Expand Down
23 changes: 14 additions & 9 deletions lib/PHPExif/Mapper/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ public function mapRawData(array $data)
}
break;
case self::EXPOSURETIME:
// normalize ExposureTime
// on one test image, it reported "10/300" instead of "1/30"
list($counter, $denominator) = explode('/', $value);
if (intval($counter) !== 1) {
$denominator /= $counter;
if (!is_float($value)) {
$value = $this->normalizeComponent($value);
}

// Based on the source code of Exiftool (PrintExposureTime subroutine):
// http://cpansearch.perl.org/src/EXIFTOOL/Image-ExifTool-9.90/lib/Image/ExifTool/Exif.pm
if ($value < 0.25001 && $value > 0) {
$value = sprintf('1/%d', intval(0.5 + 1 / $value));
} else {
$value = sprintf('%.1f', $value);
$value = preg_replace('/.0$/', '', $value);
}
$value = '1/' . round($denominator);
break;
case self::FOCALLENGTH:
$parts = explode('/', $value);
Expand Down Expand Up @@ -217,7 +222,7 @@ protected function isSection($field)
*/
protected function extractGPSCoordinate(array $components)
{
$components = array_map(array($this, 'normalizeGPSComponent'), $components);
$components = array_map(array($this, 'normalizeComponent'), $components);

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

/**
* Normalize GPS coordinates components
* Normalize component
*
* @param mixed $component
* @return int|float
*/
protected function normalizeGPSComponent($component)
protected function normalizeComponent($component)
{
$parts = explode('/', $component);

Expand Down
17 changes: 13 additions & 4 deletions tests/PHPExif/ExifTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,19 @@ public function testGetExposure()
*/
public function testGetExposureMilliseconds()
{
$expected = 1/320;
$data[\PHPExif\Exif::EXPOSURE] = '1/320';
$this->exif->setData($data);
$this->assertEquals($expected, $this->exif->getExposureMilliseconds());
$rawData = array(
array(1/300, '1/300'),
array(0.0025, 0.0025),
);

foreach ($rawData as $data) {
$expected = reset($data);
$value = end($data);

$data[\PHPExif\Exif::EXPOSURE] = $value;
$this->exif->setData($data);
$this->assertEquals($expected, $this->exif->getExposureMilliseconds());
}
}

/**
Expand Down
13 changes: 10 additions & 3 deletions tests/PHPExif/Mapper/ExiftoolMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,19 @@ public function testMapRawDataCorrectlyIgnoresIncorrectCreationDate()
public function testMapRawDataCorrectlyFormatsExposureTime()
{
$rawData = array(
\PHPExif\Mapper\Exiftool::EXPOSURETIME => 1/400,
'1/30' => 10/300,
'1/400' => 2/800,
'1/400' => 1/400,
'0' => 0,
);

$mapped = $this->mapper->mapRawData($rawData);
foreach ($rawData as $expected => $value) {
$mapped = $this->mapper->mapRawData(array(
\PHPExif\Mapper\Exiftool::EXPOSURETIME => $value,
));

$this->assertEquals('1/400', reset($mapped));
$this->assertEquals($expected, reset($mapped));
}
}

/**
Expand Down
36 changes: 33 additions & 3 deletions tests/PHPExif/Mapper/NativeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,19 @@ public function testMapRawDataCorrectlyIgnoresIncorrectDateTimeOriginal()
public function testMapRawDataCorrectlyFormatsExposureTime()
{
$rawData = array(
\PHPExif\Mapper\Native::EXPOSURETIME => '2/800',
'1/30' => 10/300,
'1/400' => 2/800,
'1/400' => 1/400,
'0' => 0,
);

$mapped = $this->mapper->mapRawData($rawData);
foreach ($rawData as $expected => $value) {
$mapped = $this->mapper->mapRawData(array(
\PHPExif\Mapper\Native::EXPOSURETIME => $value,
));

$this->assertEquals('1/400', reset($mapped));
$this->assertEquals($expected, reset($mapped));
}
}

/**
Expand Down Expand Up @@ -251,4 +258,27 @@ public function testMapRawDataCorrectlyIgnoresInvalidCreateDate()
$result
);
}

/**
* @group mapper
* @covers \PHPExif\Mapper\Native::normalizeComponent
*/
public function testNormalizeComponentCorrectly()
{
$reflMethod = new \ReflectionMethod('\PHPExif\Mapper\Native', 'normalizeComponent');
$reflMethod->setAccessible(true);

$rawData = array(
'2/800' => 0.0025,
'1/400' => 0.0025,
'0/1' => 0,
'0' => 0,
);

foreach ($rawData as $value => $expected) {
$normalized = $reflMethod->invoke($this->mapper, $value);

$this->assertEquals($expected, $normalized);
}
}
}