Skip to content

Commit

Permalink
Exif Reader IMplementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar65 committed Aug 12, 2020
1 parent a360c06 commit a74d422
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGalleryMetadata\Model\Jpeg\Segment;

use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface;
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterfaceFactory;
use Magento\MediaGalleryMetadataApi\Model\FileInterface;
use Magento\MediaGalleryMetadataApi\Model\ReadMetadataInterface;
use Magento\MediaGalleryMetadataApi\Model\SegmentInterface;

/**
* Jpeg EXIF Reader
*/
class ReadExif implements ReadMetadataInterface
{
private const EXIF_SEGMENT_NAME = 'APP1';
private const EXIF_SEGMENT_START = "Exif\x00";
private const EXIF_DATA_START_POSITION = 0;

/**
* @var MetadataInterfaceFactory
*/
private $metadataFactory;

/**
* @param MetadataInterfaceFactory $metadataFactory
*/
public function __construct(
MetadataInterfaceFactory $metadataFactory
) {
$this->metadataFactory = $metadataFactory;
}

/**
* @inheritdoc
*/
public function execute(FileInterface $file): MetadataInterface
{
$title = null;
$description = null;
$keywords = [];

foreach ($file->getSegments() as $segment) {
if ($this->isExifSegment($segment)) {
}
}
return $this->metadataFactory->create([
'title' => $title,
'description' => $description,
'keywords' => !empty($keywords) ? $keywords : null
]);
}

/**
* Does segment contain Exif data
*
* @param SegmentInterface $segment
* @return bool
*/
private function isExifSegment(SegmentInterface $segment): bool
{
return $segment->getName() === self::EXIF_SEGMENT_NAME
&& strncmp(
substr($segment->getData(), self::EXIF_DATA_START_POSITION, 4),
self::EXIF_SEGMENT_START,
self::EXIF_DATA_START_POSITION
) == 0;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/code/Magento/MediaGalleryMetadata/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<argument name="segmentReaders" xsi:type="array">
<item name="xmp" xsi:type="object">Magento\MediaGalleryMetadata\Model\Jpeg\Segment\ReadXmp</item>
<item name="iptc" xsi:type="object">Magento\MediaGalleryMetadata\Model\Jpeg\Segment\ReadIptc</item>
<item name="exif" xsi:type="object">Magento\MediaGalleryMetadata\Model\Jpeg\Segment\ReadExif</item>
</argument>
</arguments>
</virtualType>
Expand Down

0 comments on commit a74d422

Please sign in to comment.