Skip to content

Technical Design: Images metadata processing

Sergii Ivashchenko edited this page Jul 2, 2020 · 9 revisions

Module

Magento\MediaGalleryMetadata module should be responsible for images metadata processing

Functionality

MediaGalleryMetadata module should provide an ability to extract the metadata from file and populating Media Asset entity fields when an image is uploaded to Magento.

MediaGalleryMetadata module should provide an ability to update the metadata stored in an image file.

API

image

Two API service and one data interfaces should be introduced in the Magento\MediaGalleryMetadataApi module and implemented in Magento\MediaGalleryMetadata module.

Data:

  • MetadataInterface extending Magento\Framework\Api\ExtensibleDataInterface
    • getTitle(): string
    • getDescription(): string
    • getKeywords(): array

Services:

  • ExtractMetadataInterace::execute(string $path): MetadataInterface retrieve the metadata from the file
  • AddMetadataInterace::execute(string $path, MetadataInterface $metadata): void update the metadata in the file

Exceptions

ExtractMetadataInterace does not throw any exceptions, empty metadata object is returned if metadata cannot be retrieved LocalizedException should be thrown by AddMetadataInterace if the metadata cannot be retrieved or saved

Supported file formats

Magento media gallery supports jpeg, gif and png formats

  • JPEG images can have the metadata saved in EXIF, XMP and IPTC formats.
  • PNG images can have the metadata saved in EXIF, XMP and IPTC formats.
  • GIF images can have the metadata saved only in XMP format.

Implementation

The metadata reading/writing should work with the IPTC, XMP and EXIF file segments.

Retrieving Metadata

The process of retrieving metadata from a file includes the following stages:

  • Extract Metadata Service is running through all available metadata Extractors from the Extractors Pool
  • Each Extractor is checking with it's File Reader if it can read the file
  • If the file can be read, the File Reader is parsing the file into Segment Objects wrapping them into File Object
  • Extractor then sends the File Object to all the available to this extractor Segment Readers
  • Each Segment Reader is:
    • Retrieving appropriate Segment Object from File Object
    • Extracting metadata from Segment Object data and returning it as a Metadata Object
  • Extractor accumulates the data received from the Segment Readers and as soon as all needed metadata fields are populated returns the Metadata Object
  • Extract Metadata Service is accumulating the data received from the Extractors and as soon as all needed metadata fields are populated returns the Metadata Object

image

Terms:

  • "Extract Metadata Service" - Magento\MediaGalleryMetadata\Model\ExtractMetadata
  • "Extractors Pool" - Magento\MediaGalleryMetadataApi\Model\ExtractMetadataPool
  • "Extractor" - virtual type based on the Magento\MediaGalleryMetadata\Model\File\ExtractMetadata
  • "File Reader" - Magento\MediaGalleryMetadataApi\Model\FileReaderInterface
  • "Segment Reader" - Magento\MediaGalleryMetadataApi\Model\MetadataReaderInterface
  • "File Object" - Magento\MediaGalleryMetadataApi\Model\FileInterface
  • "Segment Object" - Magento\MediaGalleryMetadataApi\Model\SegmentInterface
  • "Metadata Object" - Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface

Configuration

The Extractor can be created as a virtual type based on Magento\MediaGalleryMetadata\Model\File\ExtractMetadata class specifying the file reader and segment readers.

The Extractor can be added to the Extractors Pool using DI configuration.

Saving Metadata

The process of retrieving metadata from a file includes the following stages:

  • Add Metadata Service is running through all available Metadata Updaters from the Metadata Updaters Pool
  • Each Metadata Updater is retrieving the File Object using File Reader
  • Then Metadata Updater is calling all the Segment Writers passing the File Object and Metadata Object
  • Segment Writers are adding the data from Metadata Object to the corresponding segment in the File Object and return the updated object
  • After all the Segment Writers are done, the File Object is saved to the filesystem by the File Writer

Terms:

  • "Add Metadata Service" - Magento\MediaGalleryMetadata\Model\AddMetadata
  • "Metadata Updaters Pool" - Magento\MediaGalleryMetadataApi\Model\AddMetadataPool
  • "Metadata Updater" - virtual type based on the Magento\MediaGalleryMetadata\Model\File\AddMetadata
  • "File Writer" - Magento\MediaGalleryMetadataApi\Model\FileWriterInterface
  • "Segment Writer" - Magento\MediaGalleryMetadataApi\Model\MetadataWriterInterface

Configuration

The Metadata Updater can be created as a virtual type based on Magento\MediaGalleryMetadata\Model\File\AddMetadata class specifying the file reader, file writer and segment writers.

The Metadata Updater can be added to the Metadata Updaters Pool using DI configuration.

Integration

MediaGalleryMetadata should be used in:

  • MediaGallerySynchronization module to populate the fields of synchronized images
  • MediaGalleryUi module
    • to populate the fields of images uploaded from the media gallery
    • to update metadata in the image file when the metadata is edited on the frontend
  • MediaGalleryIntegration module to populate the fields of images uploaded outside of media gallery
Clone this wiki locally