.../media/src/Plugin/media/Source/Image.php | 86 ++++++++++++++++------ .../FunctionalJavascript/MediaSourceImageTest.php | 2 - .../standard/config/optional/media.type.image.yml | 4 +- 3 files changed, 66 insertions(+), 26 deletions(-) diff --git a/core/modules/media/src/Plugin/media/Source/Image.php b/core/modules/media/src/Plugin/media/Source/Image.php index 706e180..aacb1ac 100644 --- a/core/modules/media/src/Plugin/media/Source/Image.php +++ b/core/modules/media/src/Plugin/media/Source/Image.php @@ -29,20 +29,62 @@ class Image extends File { /** - * Key for width metadata attribute. + * Key for "image width" metadata attribute. * - * @var int + * @var string */ const METADATA_ATTRIBUTE_WIDTH = 'width'; /** - * Key for height metadata attribute. + * Key for "image height" metadata attribute. * - * @var int + * @var string */ const METADATA_ATTRIBUTE_HEIGHT = 'height'; /** + * Key for "camera model" metadata attribute. + * + * @var string + */ + const METADATA_ATTRIBUTE_MODEL = 'model'; + + /** + * Key for "image creation date & time" metadata attribute. + * + * @var string + */ + const METADATA_ATTRIBUTE_CREATED = 'created'; + + /** + * Key for "ISO" metadata attribute. + * + * @var string + */ + const METADATA_ATTRIBUTE_ISO = 'iso'; + + /** + * Key for "exposure time" metadata attribute. + * + * @var string + */ + const METADATA_ATTRIBUTE_EXPOSURE = 'exposure'; + + /** + * Key for "aperture" metadata attribute. + * + * @var string + */ + const METADATA_ATTRIBUTE_APERTURE = 'aperture'; + + /** + * Key for "focal length" metadata attribute. + * + * @var string + */ + const METADATA_ATTRIBUTE_FOCAL_LENGTH = 'focal_length'; + + /** * The image factory service. * * @var \Drupal\Core\Image\ImageFactory @@ -122,12 +164,12 @@ public function getMetadataAttributes() { if ($this->canReadExifData()) { $attributes += [ - 'model' => $this->t('Camera model'), - 'created' => $this->t('Image creation datetime'), - 'iso' => $this->t('ISO'), - 'exposure' => $this->t('Exposure time'), - 'aperture' => $this->t('Aperture value'), - 'focal_length' => $this->t('Focal length'), + static::METADATA_ATTRIBUTE_MODEL => $this->t('Camera model'), + static::METADATA_ATTRIBUTE_CREATED => $this->t('Image creation date & time'), + static::METADATA_ATTRIBUTE_ISO => $this->t('ISO'), + static::METADATA_ATTRIBUTE_EXPOSURE => $this->t('Exposure time'), + static::METADATA_ATTRIBUTE_APERTURE => $this->t('Aperture value'), + static::METADATA_ATTRIBUTE_FOCAL_LENGTH => $this->t('Focal length'), ]; } @@ -161,24 +203,24 @@ public function getMetadata(MediaInterface $media, $name) { if ($this->canReadExifData()) { switch ($name) { - case 'created': + case static::METADATA_ATTRIBUTE_CREATED: /** @var \DateTime $date */ $date = new DrupalDateTime($this->getExifField($uri, 'DateTimeOriginal')); return $date->getTimestamp(); - case 'model': + case static::METADATA_ATTRIBUTE_MODEL: return $this->getExifField($uri, 'Model'); - case 'iso': + case static::METADATA_ATTRIBUTE_ISO: return $this->getExifField($uri, 'ISOSpeedRatings'); - case 'exposure': + case static::METADATA_ATTRIBUTE_EXPOSURE: return $this->getExifField($uri, 'ExposureTime'); - case 'aperture': + case static::METADATA_ATTRIBUTE_APERTURE: return $this->getExifField($uri, 'FNumber'); - case 'focal_length': + case static::METADATA_ATTRIBUTE_FOCAL_LENGTH: return $this->getExifField($uri, 'FocalLength'); } } @@ -216,22 +258,22 @@ protected function canReadExifData() { * * @param string $uri * The URI for the file that we are getting the EXIF for. - * @param string $field + * @param string $exif_field * The name of the requested EXIF field. * * @return string|bool * The value for the requested field or FALSE if it is not set. */ - protected function getExifField($uri, $field) { + protected function getExifField($uri, $exif_field) { if (empty($this->exif)) { - $this->exif = $this->getExifData($uri); + $this->exif = $this->getAllExifFieldValues($uri); } - return $this->exif[$field] ?: NULL; + return $this->exif[$exif_field] ?: NULL; } /** - * Reads EXIF. + * Gets all fields from the EXIF section of the given file. * * @param string $uri * The URI for the file that we are getting the EXIF field value. @@ -241,7 +283,7 @@ protected function getExifField($uri, $field) { * the values associated with those headers. We return FALSE if the data * can't be read. */ - protected function getExifData($uri) { + protected function getAllExifFieldValues($uri) { return exif_read_data($this->fileSystem->realpath($uri), 'EXIF'); } diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php index bfe1f83..ea8572e 100644 --- a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php @@ -7,8 +7,6 @@ /** * Tests the image media source. * - * @package Drupal\Tests\media\FunctionalJavascript - * * @group media */ class MediaSourceImageTest extends MediaSourceTestBase { diff --git a/core/profiles/standard/config/optional/media.type.image.yml b/core/profiles/standard/config/optional/media.type.image.yml index baea949..cb96e20 100644 --- a/core/profiles/standard/config/optional/media.type.image.yml +++ b/core/profiles/standard/config/optional/media.type.image.yml @@ -5,10 +5,10 @@ dependencies: - media id: image label: Image -description: 'Use the "Image" media type for storing image media locally.' +description: "Use local images for reusable media." source: image queue_thumbnail_downloads: false -new_revision: false +new_revision: true source_configuration: source_field: field_media_image field_map: { }