diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php index 0c142403affd1..5e5deb0e119fc 100644 --- a/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php +++ b/app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php @@ -41,14 +41,44 @@ public function __construct( */ public function execute(FileInterface $file): MetadataInterface { - $title = null; - $description = null; - $keywords = []; + if (!is_callable('exif_read_data')) { + throw new LocalizedException( + __('exif_read_data() must be enabled in php configuration') + ); + } foreach ($file->getSegments() as $segment) { if ($this->isExifSegment($segment)) { + return $this->getExifData($file->getPath()); } } + + return $this->metadataFactory->create([ + 'title' => null, + 'description' => null, + 'keywords' => null + ]); + } + + /** + * Parese exif data from segment + * + * @param string $filePath + */ + private function getExifData(string $filePath): MetadataInterface + { + $title = null; + $description = null; + $keywords = []; + + $data = exif_read_data($filePath); + + if ($data) { + $title = isset($data['DocumentName']) ? $data['DocumentName'] : null; + $description = isset($data['ImageDescription']) ? $data['ImageDescription'] : null; + $keywords = ''; + } + return $this->metadataFactory->create([ 'title' => $title, 'description' => $description, diff --git a/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php b/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php new file mode 100644 index 0000000000000..cd1160ed92589 --- /dev/null +++ b/app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php @@ -0,0 +1,91 @@ +metadataFactory = $metadataFactory; + } + + /** + * @inheritdoc + */ + public function execute(FileInterface $file): MetadataInterface + { + foreach ($file->getSegments() as $segment) { + if ($this->isExifSegment($segment)) { + return $this->getExifData($segment); + } + } + + return $this->metadataFactory->create([ + 'title' => null, + 'description' => null, + 'keywords' => null + ]); + } + + /** + * Parese exif data from segment + * + * @param FileInterface $filePath + */ + private function getExifData(SegmentInterface $segment): MetadataInterface + { + $title = null; + $description = null; + $keywords = []; + + $data = exif_read_data('data://image/jpeg;base64,' . base64_encode($segment->getData())); + + if ($data) { + $title = isset($data['DocumentName']) ? $data['DocumentName'] : null; + $description = isset($data['ImageDescription']) ? $data['ImageDescription'] : null; + $keywords = ''; + } + + 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; + } +} diff --git a/app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/ExtractMetadataTest.php b/app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/ExtractMetadataTest.php index a5ff5de0aeaef..ebe96183eb1f2 100644 --- a/app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/ExtractMetadataTest.php +++ b/app/code/Magento/MediaGalleryMetadata/Test/Integration/Model/ExtractMetadataTest.php @@ -37,14 +37,14 @@ protected function setUp(): void * @param string $fileName * @param string $title * @param string $description - * @param array $keywords + * @param null|array $keywords * @throws LocalizedException */ public function testExecute( string $fileName, string $title, string $description, - array $keywords + ?array $keywords ): void { $path = realpath(__DIR__ . '/../../_files/' . $fileName); $metadata = $this->extractMetadata->execute($path); @@ -62,60 +62,63 @@ public function testExecute( public function filesProvider(): array { return [ + [ + 'exif_image.png', + 'Exif title png imge', + 'Exif description png imge', + null + ], [ 'exif-image.jpeg', + 'Exif Magento title', + 'Exif description metadata', + null + ], + [ + 'macos-photos.jpeg', + 'Title of the magento image', + 'Description of the magento image', + [ + 'magento', + 'mediagallerymetadata' + ] + ], + [ + 'macos-preview.png', + 'Title of the magento image', + 'Description of the magento image', + [ + 'magento', + 'mediagallerymetadata' + ] + ], + [ + 'iptc_only.jpeg', + 'Title of the magento image', + 'Description of the magento image', + [ + 'magento', + 'mediagallerymetadata' + ] + ], + [ + 'exiftool.gif', + 'Title of the magento image', + 'Description of the magento image', + [ + 'magento', + 'mediagallerymetadata' + ] + ], + [ + 'iptc_only.png', 'Title of the magento image', - 'exif fromat title', + 'PNG format is awesome', [ - 'exif', + 'png', 'awesome' ] ], - //[ - // 'macos-photos.jpeg', - // 'Title of the magento image', - // 'Description of the magento image', - // [ - // 'magento', - // 'mediagallerymetadata' - // ] - //], - // [ - // 'macos-preview.png', - // 'Title of the magento image', - // 'Description of the magento image', - // [ - // 'magento', - // 'mediagallerymetadata' - /// ] - // ], - // [ - // 'iptc_only.jpeg', - /// 'Title of the magento image', - // 'Description of the magento image', - // [ - // 'magento', - // 'mediagallerymetadata' - // ] - //], - //[ - // 'exiftool.gif', - // 'Title of the magento image', - // 'Description of the magento image', - // [ - // 'magento', - // 'mediagallerymetadata' - // ] - // ], - //[ - // 'iptc_only.png', - // 'Title of the magento image', - // 'PNG format is awesome', - // [ - // 'png', - // 'awesome' - // ] - ///], ]; } } diff --git a/app/code/Magento/MediaGalleryMetadata/Test/_files/exif-image.jpeg b/app/code/Magento/MediaGalleryMetadata/Test/_files/exif-image.jpeg index 666488a923b2c..cfe27433fd9fc 100644 Binary files a/app/code/Magento/MediaGalleryMetadata/Test/_files/exif-image.jpeg and b/app/code/Magento/MediaGalleryMetadata/Test/_files/exif-image.jpeg differ diff --git a/app/code/Magento/MediaGalleryMetadata/Test/_files/exif_image.png b/app/code/Magento/MediaGalleryMetadata/Test/_files/exif_image.png new file mode 100644 index 0000000000000..4a6bf30c2d516 Binary files /dev/null and b/app/code/Magento/MediaGalleryMetadata/Test/_files/exif_image.png differ diff --git a/app/code/Magento/MediaGalleryMetadata/etc/di.xml b/app/code/Magento/MediaGalleryMetadata/etc/di.xml index d6b2899729fb7..4cd9a34e43a93 100644 --- a/app/code/Magento/MediaGalleryMetadata/etc/di.xml +++ b/app/code/Magento/MediaGalleryMetadata/etc/di.xml @@ -112,6 +112,7 @@ Magento\MediaGalleryMetadata\Model\Png\Segment\ReadXmp Magento\MediaGalleryMetadata\Model\Png\Segment\ReadIptc + Magento\MediaGalleryMetadata\Model\Png\Segment\ReadExif