|
| 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 | +} |
0 commit comments