Skip to content

Commit

Permalink
Add suport of reading PNG JPEG Exif metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar65 committed Aug 17, 2020
1 parent 5c7c959 commit 4ef4c6c
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'
// ]
///],
];
}
}
Binary file modified 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.
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 @@ -112,6 +112,7 @@
<argument name="segmentReaders" xsi:type="array">
<item name="xmp" xsi:type="object">Magento\MediaGalleryMetadata\Model\Png\Segment\ReadXmp</item>
<item name="iptc" xsi:type="object">Magento\MediaGalleryMetadata\Model\Png\Segment\ReadIptc</item>
<item name="exif" xsi:type="object">Magento\MediaGalleryMetadata\Model\Png\Segment\ReadExif</item>
</argument>
</arguments>
</virtualType>
Expand Down

0 comments on commit 4ef4c6c

Please sign in to comment.