Skip to content

Commit 526668f

Browse files
committed
Add AVIF format in Exif
- Allow avif format image to get exif data
1 parent a76a6e9 commit 526668f

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

lib/PHPExif/Adapter/Native.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use function Safe\filesize;
1111
use function Safe\getimagesize;
1212
use function Safe\iptcparse;
13+
use function Safe\exec;
14+
use function Safe\json_decode;
15+
use function Safe\preg_replace;
1316

1417
/**
1518
* PHP Exif Native Reader Adapter
@@ -198,6 +201,27 @@ public function getExifFromFile(string $file): Exif
198201
$this->getIncludeThumbnail()
199202
);
200203

204+
if ($mimeType === 'image/avif') {
205+
$output_array = [];
206+
exec("exiftool -j " . escapeshellarg($file), $output_array); // Safe\exec will throw exception if fails
207+
$tempExif = [];
208+
209+
if (count($output_array) > 0) {
210+
$json = implode("\n", $output_array);
211+
$tempExif = json_decode($json, true)[0] ?? [];
212+
}
213+
214+
$data = array_merge(
215+
['FileName' => basename($file), 'MimeType' => $mimeType],
216+
$tempExif
217+
);
218+
219+
// Ensure FileSize is integer
220+
if (isset($data['FileSize'])) {
221+
$data['FileSize'] = (int) preg_replace('/\D/', '', $data['FileSize']);
222+
}
223+
}
224+
201225
// exif_read_data failed to read exif data (i.e. not a jpg/tiff)
202226
if (false === $data) {
203227
$data = [];

tests/PHPExif/Adapter/ExiftoolTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ public function testGetExifFromFileWithUtf8()
112112
$this->assertNotEmpty($result->getRawData());
113113
}
114114

115+
/**
116+
* @group exiftool
117+
*/
118+
public function testGetExifFromFileWithAvif()
119+
{
120+
$file = PHPEXIF_TEST_ROOT . '/files/fox.profile0.10bpc.yuv420.avif';
121+
$this->adapter->setOptions(array('encoding' => array('utf8')));
122+
$result = $this->adapter->getExifFromFile($file);
123+
$this->assertInstanceOf('\PHPExif\Exif', $result);
124+
$this->assertIsArray($result->getRawData());
125+
$this->assertNotEmpty($result->getRawData());
126+
}
127+
115128
/**
116129
* @group exiftool
117130
*/

tests/PHPExif/ExifTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,5 +788,20 @@ public function testAdapterConsistency()
788788
);
789789
}
790790
}
791+
792+
// Test for .avif type image for Native adapter
793+
$testfiles = array(
794+
PHPEXIF_TEST_ROOT . '/files/fox.profile0.10bpc.yuv420.avif',
795+
);
796+
797+
foreach ($testfiles as $file) {
798+
$file_name = basename($file);
799+
$adapter_native = new \PHPExif\Adapter\Native();
800+
$result_exif_native = $adapter_native->getExifFromFile($file);
801+
802+
$this->assertEquals($file_name, $result_exif_native->getFileName());
803+
$this->assertEquals('image/avif', $result_exif_native->getMimeType());
804+
$this->assertIsNumeric($result_exif_native->getWidth());
805+
}
791806
}
792807
}
81.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)