Skip to content

Commit e43cc30

Browse files
authored
Merge pull request #63 from jaywilliams/patch-1
Fix Division By Zero Warning for Focal Length
2 parents 51add6b + 0ce7a9f commit e43cc30

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/PHPExif/Mapper/Native.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,12 @@ public function mapRawData(array $data)
165165
break;
166166
case self::FOCALLENGTH:
167167
$parts = explode('/', $value);
168-
$value = (int) reset($parts) / (int) end($parts);
168+
// Avoid division by zero if focal length is invalid
169+
if (end($parts) == '0') {
170+
$value = 0;
171+
} else {
172+
$value = (int) reset($parts) / (int) end($parts);
173+
}
169174
break;
170175
case self::XRESOLUTION:
171176
case self::YRESOLUTION:

tests/PHPExif/Mapper/NativeMapperTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,21 @@ public function testMapRawDataCorrectlyFormatsFocalLength()
138138
$this->assertEquals(6, reset($mapped));
139139
}
140140

141+
/**
142+
* @group mapper
143+
* @covers \PHPExif\Mapper\Native::mapRawData
144+
*/
145+
public function testMapRawDataCorrectlyFormatsFocalLengthDivisionByZero()
146+
{
147+
$rawData = array(
148+
\PHPExif\Mapper\Native::FOCALLENGTH => '1/0',
149+
);
150+
151+
$mapped = $this->mapper->mapRawData($rawData);
152+
153+
$this->assertEquals(0, reset($mapped));
154+
}
155+
141156
/**
142157
* @group mapper
143158
* @covers \PHPExif\Mapper\Native::mapRawData

0 commit comments

Comments
 (0)