Skip to content

Commit e60845c

Browse files
authored
Gracefully handle invalid IPTC data (#53)
1 parent a5daecb commit e60845c

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

lib/PHPExif/Adapter/ImageMagick.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPExif\Exif;
1515
use Imagick;
16+
use Safe\Exceptions\ImageException;
1617

1718
use function Safe\filesize;
1819
use function Safe\iptcparse;
@@ -105,7 +106,11 @@ public function getExifFromFile(string $file) : Exif
105106
public function getIptcData(string $profile) : array
106107
{
107108
$arrData = [];
108-
$iptc = iptcparse($profile);
109+
try {
110+
$iptc = iptcparse($profile);
111+
} catch (ImageException) {
112+
return $arrData;
113+
}
109114

110115
foreach ($this->iptcMapping as $name => $field) {
111116
if (!isset($iptc[$field])) {

lib/PHPExif/Adapter/Native.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ public function getIptcData(string $file) : array
258258
getimagesize($file, $info);
259259
$arrData = array();
260260
if (isset($info['APP13'])) {
261-
$iptc = iptcparse($info['APP13']);
261+
try {
262+
$iptc = iptcparse($info['APP13']);
263+
} catch (ImageException) {
264+
return $arrData;
265+
}
262266

263267
foreach ($this->iptcMapping as $name => $field) {
264268
if (!isset($iptc[$field])) {

tests/PHPExif/Adapter/ImageMagickTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@ public function testGetExifFromFile()
2727
$this->assertNotEmpty($result->getRawData());
2828
}
2929

30+
/**
31+
* @group ImageMagick
32+
* @covers \PHPExif\Adapter\ImageMagick::getIptcData
33+
*/
34+
public function testGetEmptyIptcData()
35+
{
36+
$result = $this->adapter->getIptcData("");
37+
38+
$this->assertEquals([], $result);
39+
}
40+
3041
}

tests/PHPExif/Adapter/NativeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ public function testGetIptcData()
145145
$this->assertEquals($expected, $result);
146146
}
147147

148+
/**
149+
* @group native
150+
* @covers \PHPExif\Adapter\Native::getIptcData
151+
*/
152+
public function testGetEmptyIptcData()
153+
{
154+
$file = PHPEXIF_TEST_ROOT . '/files/empty_iptc.jpg';
155+
$result = $this->adapter->getIptcData($file);
156+
157+
$this->assertEquals([], $result);
158+
}
159+
148160
/**
149161
* @group native
150162
* @covers \PHPExif\Adapter\Native::setSectionsAsArrays

tests/files/empty_iptc.jpg

3.14 MB
Loading

0 commit comments

Comments
 (0)