Skip to content

Commit a74d422

Browse files
committed
Exif Reader IMplementation
1 parent a360c06 commit a74d422

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaGalleryMetadata\Model\Jpeg\Segment;
9+
10+
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface;
11+
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterfaceFactory;
12+
use Magento\MediaGalleryMetadataApi\Model\FileInterface;
13+
use Magento\MediaGalleryMetadataApi\Model\ReadMetadataInterface;
14+
use Magento\MediaGalleryMetadataApi\Model\SegmentInterface;
15+
16+
/**
17+
* Jpeg EXIF Reader
18+
*/
19+
class ReadExif implements ReadMetadataInterface
20+
{
21+
private const EXIF_SEGMENT_NAME = 'APP1';
22+
private const EXIF_SEGMENT_START = "Exif\x00";
23+
private const EXIF_DATA_START_POSITION = 0;
24+
25+
/**
26+
* @var MetadataInterfaceFactory
27+
*/
28+
private $metadataFactory;
29+
30+
/**
31+
* @param MetadataInterfaceFactory $metadataFactory
32+
*/
33+
public function __construct(
34+
MetadataInterfaceFactory $metadataFactory
35+
) {
36+
$this->metadataFactory = $metadataFactory;
37+
}
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
public function execute(FileInterface $file): MetadataInterface
43+
{
44+
$title = null;
45+
$description = null;
46+
$keywords = [];
47+
48+
foreach ($file->getSegments() as $segment) {
49+
if ($this->isExifSegment($segment)) {
50+
}
51+
}
52+
return $this->metadataFactory->create([
53+
'title' => $title,
54+
'description' => $description,
55+
'keywords' => !empty($keywords) ? $keywords : null
56+
]);
57+
}
58+
59+
/**
60+
* Does segment contain Exif data
61+
*
62+
* @param SegmentInterface $segment
63+
* @return bool
64+
*/
65+
private function isExifSegment(SegmentInterface $segment): bool
66+
{
67+
return $segment->getName() === self::EXIF_SEGMENT_NAME
68+
&& strncmp(
69+
substr($segment->getData(), self::EXIF_DATA_START_POSITION, 4),
70+
self::EXIF_SEGMENT_START,
71+
self::EXIF_DATA_START_POSITION
72+
) == 0;
73+
}
74+
}
Loading

app/code/Magento/MediaGalleryMetadata/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<argument name="segmentReaders" xsi:type="array">
122122
<item name="xmp" xsi:type="object">Magento\MediaGalleryMetadata\Model\Jpeg\Segment\ReadXmp</item>
123123
<item name="iptc" xsi:type="object">Magento\MediaGalleryMetadata\Model\Jpeg\Segment\ReadIptc</item>
124+
<item name="exif" xsi:type="object">Magento\MediaGalleryMetadata\Model\Jpeg\Segment\ReadExif</item>
124125
</argument>
125126
</arguments>
126127
</virtualType>

0 commit comments

Comments
 (0)