Skip to content

Commit

Permalink
Gracefully handle invalid IPTC data (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil4 authored Aug 25, 2022
1 parent a5daecb commit e60845c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/PHPExif/Adapter/ImageMagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPExif\Exif;
use Imagick;
use Safe\Exceptions\ImageException;

use function Safe\filesize;
use function Safe\iptcparse;
Expand Down Expand Up @@ -105,7 +106,11 @@ public function getExifFromFile(string $file) : Exif
public function getIptcData(string $profile) : array
{
$arrData = [];
$iptc = iptcparse($profile);
try {
$iptc = iptcparse($profile);
} catch (ImageException) {
return $arrData;
}

foreach ($this->iptcMapping as $name => $field) {
if (!isset($iptc[$field])) {
Expand Down
6 changes: 5 additions & 1 deletion lib/PHPExif/Adapter/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,11 @@ public function getIptcData(string $file) : array
getimagesize($file, $info);
$arrData = array();
if (isset($info['APP13'])) {
$iptc = iptcparse($info['APP13']);
try {
$iptc = iptcparse($info['APP13']);
} catch (ImageException) {
return $arrData;
}

foreach ($this->iptcMapping as $name => $field) {
if (!isset($iptc[$field])) {
Expand Down
11 changes: 11 additions & 0 deletions tests/PHPExif/Adapter/ImageMagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public function testGetExifFromFile()
$this->assertNotEmpty($result->getRawData());
}

/**
* @group ImageMagick
* @covers \PHPExif\Adapter\ImageMagick::getIptcData
*/
public function testGetEmptyIptcData()
{
$result = $this->adapter->getIptcData("");

$this->assertEquals([], $result);
}

}
12 changes: 12 additions & 0 deletions tests/PHPExif/Adapter/NativeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ public function testGetIptcData()
$this->assertEquals($expected, $result);
}

/**
* @group native
* @covers \PHPExif\Adapter\Native::getIptcData
*/
public function testGetEmptyIptcData()
{
$file = PHPEXIF_TEST_ROOT . '/files/empty_iptc.jpg';
$result = $this->adapter->getIptcData($file);

$this->assertEquals([], $result);
}

/**
* @group native
* @covers \PHPExif\Adapter\Native::setSectionsAsArrays
Expand Down
Binary file added tests/files/empty_iptc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e60845c

Please sign in to comment.