-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add suport of reading PNG JPEG Exif metadata
- Loading branch information
Showing
6 changed files
with
177 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
app/code/Magento/MediaGalleryMetadata/Model/Png/Segment/ReadExif.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\MediaGalleryMetadata\Model\Png\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 = 'eXIf'; | ||
|
||
/** | ||
* @var MetadataInterfaceFactory | ||
*/ | ||
private $metadataFactory; | ||
|
||
/** | ||
* @param MetadataInterfaceFactory $metadataFactory | ||
*/ | ||
public function __construct( | ||
MetadataInterfaceFactory $metadataFactory | ||
) { | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+136 Bytes
(100%)
app/code/Magento/MediaGalleryMetadata/Test/_files/exif-image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters