diff --git a/core/modules/media/config/schema/media.schema.yml b/core/modules/media/config/schema/media.schema.yml index b326f9b..e391f58 100644 --- a/core/modules/media/config/schema/media.schema.yml +++ b/core/modules/media/config/schema/media.schema.yml @@ -40,6 +40,10 @@ media.handler.file: type: media.handler.field_aware label: 'File handler configuration' +media.handler.image: + type: media.handler.field_aware + label: 'Image handler configuration' + action.configuration.media_delete_action: type: action_configuration_default label: 'Delete media configuration' diff --git a/core/modules/media/images/icons/no-thumbnail.png b/core/modules/media/images/icons/no-thumbnail.png new file mode 100644 index 0000000..c50e3c7 --- /dev/null +++ b/core/modules/media/images/icons/no-thumbnail.png @@ -0,0 +1,6 @@ +PNG + + IHDRetEXtSoftwareAdobe ImageReadyqe<0IDATx1O"[ݛb6TRQYA4Ra쬬 +o +=+2,sȲ{z4wf<3~zߤe#Cp!8CCp!8CpHp!8Cp !8Cp!8$8Cp!8C[sjvoWRQ.7 x}}}~~ެM}ʟZ>f8Vl.tJҊq K+t)d%.tp.~8rh4pDa[%H>XpG 8 8 8 8 8 8 8v ˫fpcy'''Jpcy".pt:y9pg`Sן +Gp DFk]7_Q*:NőFC?JjZQx /߿ñ t-F$cEFbqttG:[uRZdőWaEEF8Tq{{;L$͑@p"#ɘ_X]d±4D(Ԏm%(d±sd c,ZV:S.|qZz-GDGh4 #'U׶Ñ8]/pNFp⎪鷀{H­\<# ]k"HҜ[^WZM>,NX^^/|x:uss3W*|l {g=o9S8"p<(s#v2Y#q8j9>Ҳ4]xyKr},|\yqtmAp,Yl-h[[y{{[롴?Z\N L988G%^l!cEp!8CpHp!8Cp !8Cp!8$8Cp!8Cp!8CpmA 0{b~aIENDB` \ No newline at end of file diff --git a/core/modules/media/src/Plugin/media/Handler/Image.php b/core/modules/media/src/Plugin/media/Handler/Image.php new file mode 100644 index 0000000..278d643 --- /dev/null +++ b/core/modules/media/src/Plugin/media/Handler/Image.php @@ -0,0 +1,246 @@ +imageFactory = $image_factory; + $this->fileSystem = $file_system; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('entity_field.manager'), + $container->get('config.factory'), + $container->get('plugin.manager.field.field_type'), + $container->get('image.factory'), + $container->get('file_system') + ); + } + + /** + * {@inheritdoc} + */ + public function getProvidedFields() { + $fields = parent::getProvidedFields(); + + $fields += [ + 'width' => ['label' => $this->t('Width')], + 'height' => ['label' => $this->t('Height')], + ]; + + if ($this->canReadExifData()) { + $fields += [ + 'model' => ['label' => $this->t('Camera model')], + 'created' => ['label' => $this->t('Image creation datetime')], + 'iso' => ['label' => $this->t('Iso')], + 'exposure' => ['label' => $this->t('Exposure time')], + 'aperture' => ['label' => $this->t('Aperture value')], + 'focal_length' => ['label' => $this->t('Focal length')], + ]; + } + + return $fields; + } + + /** + * {@inheritdoc} + */ + public function getField(MediaInterface $media, $name) { + $value = parent::getField($media, $name); + + if ($value !== FALSE) { + return $value; + } + + // Get the file, image and EXIF data. + $file = $this->getSourceFieldValue($media); + $uri = $file->getFileUri(); + $image = $this->imageFactory->get($uri); + + // Return the field. + switch ($name) { + case 'width': + return $image->getWidth() ?: FALSE; + + case 'height': + return $image->getHeight() ?: FALSE; + } + + if ($this->canReadExifData()) { + switch ($name) { + case 'created': + /** @var \DateTime $date */ + $date = new DrupalDateTime($this->getExifField($uri, 'DateTimeOriginal')); + return $date->getTimestamp(); + + case 'model': + return $this->getExifField($uri, 'Model'); + + case 'iso': + return $this->getExifField($uri, 'ISOSpeedRatings'); + + case 'exposure': + return $this->getExifField($uri, 'ExposureTime'); + + case 'aperture': + return $this->getExifField($uri, 'FNumber'); + + case 'focal_length': + return $this->getExifField($uri, 'FocalLength'); + } + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form = parent::buildConfigurationForm($form, $form_state); + + // Show message if it's not possible to read EXIF data. + if (!$this->canReadExifData()) { + $form['no_exif_data_reader'] = [ + '#markup' => $this->t('Unable to read EXIF data for Image. In order to provide EXIF data reading functionality please take a look at PHP documentation of exif_read_data function.'), + ]; + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function getDefaultThumbnail() { + return $this->configFactory->get('media.settings') + ->get('icon_base') . '/no-thumbnail.png'; + } + + /** + * {@inheritdoc} + */ + public function getThumbnail(MediaInterface $media) { + return $this->getSourceFieldValue($media)->getFileUri(); + } + + /** + * Check does functionality for reading EXIF data exist. + * + * @return bool + * Returns TRUE if functionality for reading of EXIF data is provided. + */ + protected function canReadExifData() { + return function_exists('exif_read_data'); + } + + /** + * Get EXIF field value. + * + * @param string $uri + * The uri for the file that we are getting the EXIF. + * @param string $field + * The name of the EXIF field. + * + * @return string|bool + * The value for the requested field or FALSE if is not set. + */ + protected function getExifField($uri, $field) { + if (empty($this->exif)) { + $this->exif = $this->getExifData($uri); + } + + return $this->exif[$field] ?: FALSE; + } + + /** + * Read EXIF. + * + * @param string $uri + * The uri for the file that we are getting the Exif. + * + * @return array|bool + * An associative array where the array indexes are the header names and + * the array values are the values associated with those headers or FALSE + * if the data can't be read. + */ + protected function getExifData($uri) { + return exif_read_data($this->fileSystem->realpath($uri), 'EXIF'); + } + +} diff --git a/core/modules/media/tests/fixtures/exif_example.jpeg b/core/modules/media/tests/fixtures/exif_example.jpeg new file mode 100644 index 0000000..2cba9b9 --- /dev/null +++ b/core/modules/media/tests/fixtures/exif_example.jpeg @@ -0,0 +1,50 @@ +JFIFHHExifMM*bv~(i%Drupal EXIF CameraHH02100100NE4` Əhttp://ns.adobe.com/xap/1.0/ + + + + + Drupal EXIF Camera + 72 + 72 + Inch + Centered + Exif Version 2.1 + FlashPix Version 1.0 + Uncalibrated + N + 52, 30, 32.751 + E + 13, 22, 25.5432 + + + + + +C + + +    C   Y HNƋ`:Q&Y9nZl(-p㓟{'h\R@o3uv'N%Wi}cʭ0E-%+[}[/OI^fdp,Ć^_?}:ΞQ]gYh2Z]ՋEgDh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AQaq @0P?;Y(>*Tʯ $s pF-JJ$4 Z`ZiHM|AD yb0[f)ۿG]W&c^gXTRW @HD)QiϑP1 +0FTv3^1*x4}!meKTUniK GQ&6L~]-d(%᎐qS!rU^qtQv P9@khKKwfD5j;{ޏMGuChV@1Qކa#6 r^~FFǒhbE;K5?YcB2G&ƃgWgPlR $field_name, + 'entity_type' => 'media', + 'type' => $field_type, + ]); + $storage->save(); + + $config = FieldConfig::create([ + 'field_storage' => $storage, + 'bundle' => $type_id, + ]); + $config->save(); + + // Make the field visible in the form display. + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ + $component = \Drupal::service('plugin.manager.field.widget') + ->prepareConfiguration($field_type, []); + + entity_get_form_display('media', $type_id, 'default') + ->setComponent($field_name, $component) + ->save(); + } + + /** + * Helper to create a set of fields in a media type. + * + * @param array $fields + * An associative array where keys are field names and values field types. + * @param string $type_id + * The type machine name. + */ + protected function createMediaFields(array $fields, $type_id) { + foreach ($fields as $field_name => $field_type) { + $this->createMediaField($field_name, $field_type, $type_id); + } + } + + /** + * Just dummy test to check generic methods. + */ + public function testMediaImagePlugin() { + $type_name = 'test_media_image_type'; + $source_field_id = 'field_media_image'; + $provided_fields = [ + 'mime', + 'width', + 'height', + 'size', + 'created', + 'model', + 'iso', + 'exposure', + 'aperture', + 'focal_length', + ]; + + $session = $this->getSession(); + $page = $session->getPage(); + $assert_session = $this->assertSession(); + + // Create image media handler. + $this->createMediaTypeTest($type_name, 'image', $provided_fields); + + // Create a supported and a non-supported field. + $fields = [ + 'field_string_mime' => 'string', + 'field_string_width' => 'string', + 'field_string_model' => 'string', + ]; + $this->createMediaFields($fields, $type_name); + + // Adjust the allowed extensions on the file field. + $image_field = FieldConfig::load("media.{$type_name}.{$source_field_id}"); + $image_field->setSetting('file_extensions', 'png jpeg')->save(); + + // Hide the media name to test default name generation. + $this->hideMediaField('name', $type_name); + + $this->drupalGet("admin/structure/media/manage/$type_name"); + $this->assertSelectOptions("handler_configuration[image][source_field]", + [$source_field_id], + ['field_string_mime', 'field_string_width', 'field_string_model'] + ); + $page->selectFieldOption("field_mapping[mime]", 'field_string_mime'); + $page->selectFieldOption("field_mapping[width]", 'field_string_width'); + $page->selectFieldOption("field_mapping[model]", 'field_string_model'); + $page->pressButton('Save'); + + // Create a media item. + $this->drupalGet("media/add/{$type_name}"); + $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::root() . '/core/modules/media/tests/fixtures/exif_example.jpeg'); + $assert_session->assertWaitOnAjaxRequest(); + $page->fillField("{$source_field_id}[0][alt]", 'EXIF Image Alt Text'); + $page->pressButton('Save and publish'); + + $assert_session->addressEquals('media/1'); + + // Make sure the thumbnail is created from uploaded image. + $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'exif_example.jpeg'); + + // Load the media and check that all fields are properly populated. + $media = Media::load(1); + $this->assertEquals('exif_example.jpeg', $media->label()); + $this->assertEquals('Drupal EXIF Camera', $media->get('field_string_model')->value); + $this->assertEquals('200', $media->get('field_string_width')->value); + $this->assertEquals('image/jpeg', $media->get('field_string_mime')->value); + } + +} diff --git a/core/profiles/standard/config/optional/core.entity_form_display.media.image.default.yml b/core/profiles/standard/config/optional/core.entity_form_display.media.image.default.yml new file mode 100644 index 0000000..a040f31 --- /dev/null +++ b/core/profiles/standard/config/optional/core.entity_form_display.media.image.default.yml @@ -0,0 +1,46 @@ +langcode: en +status: true +dependencies: + config: + - field.field.media.image.field_media_image + - image.style.thumbnail + - media.type.image + module: + - image +id: media.image.default +targetEntityType: media +bundle: image +mode: default +content: + created: + type: datetime_timestamp + weight: 10 + region: content + settings: { } + third_party_settings: { } + field_media_image: + settings: + progress_indicator: throbber + preview_image_style: thumbnail + third_party_settings: { } + type: image_image + weight: 26 + region: content + name: + type: string_textfield + weight: -5 + region: content + settings: + size: 60 + placeholder: '' + third_party_settings: { } + uid: + type: entity_reference_autocomplete + weight: 5 + settings: + match_operator: CONTAINS + size: 60 + placeholder: '' + region: content + third_party_settings: { } +hidden: { } diff --git a/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml b/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml new file mode 100644 index 0000000..e541d7f --- /dev/null +++ b/core/profiles/standard/config/optional/core.entity_view_display.media.image.default.yml @@ -0,0 +1,51 @@ +langcode: en +status: true +dependencies: + config: + - field.field.media.image.field_media_image + - image.style.thumbnail + - media.type.image + module: + - image + - user +id: media.image.default +targetEntityType: media +bundle: image +mode: default +content: + created: + label: hidden + type: timestamp + weight: 0 + region: content + settings: + date_format: medium + custom_date_format: '' + timezone: '' + third_party_settings: { } + field_media_image: + label: above + settings: + image_style: '' + image_link: '' + third_party_settings: { } + type: image + weight: 6 + region: content + thumbnail: + type: image + weight: 5 + label: hidden + settings: + image_style: thumbnail + image_link: '' + region: content + third_party_settings: { } + uid: + label: hidden + type: author + weight: 0 + region: content + settings: { } + third_party_settings: { } +hidden: { } diff --git a/core/profiles/standard/config/optional/field.field.media.image.field_media_image.yml b/core/profiles/standard/config/optional/field.field.media.image.field_media_image.yml new file mode 100644 index 0000000..628c386 --- /dev/null +++ b/core/profiles/standard/config/optional/field.field.media.image.field_media_image.yml @@ -0,0 +1,40 @@ +langcode: en +status: true +dependencies: + config: + - field.storage.media.field_media_image + - media.type.image + module: + - image + enforced: + module: + - media +id: media.image.field_media_image +field_name: field_media_image +entity_type: media +bundle: image +label: field_media_image +description: '' +required: false +translatable: true +default_value: { } +default_value_callback: '' +settings: + file_extensions: 'png gif jpg jpeg' + alt_field: true + alt_field_required: true + title_field: false + title_field_required: false + max_resolution: '' + min_resolution: '' + default_image: + uuid: null + alt: '' + title: '' + width: null + height: null + file_directory: '[date:custom:Y]-[date:custom:m]' + max_filesize: '' + handler: 'default:file' + handler_settings: { } +field_type: image diff --git a/core/profiles/standard/config/optional/field.storage.media.field_media_image.yml b/core/profiles/standard/config/optional/field.storage.media.field_media_image.yml new file mode 100644 index 0000000..1597ce6 --- /dev/null +++ b/core/profiles/standard/config/optional/field.storage.media.field_media_image.yml @@ -0,0 +1,32 @@ +langcode: en +status: true +dependencies: + module: + - file + - image + - media + enforced: + module: + - media +id: media.field_media_image +field_name: field_media_image +entity_type: media +type: image +settings: + default_image: + uuid: null + alt: '' + title: '' + width: null + height: null + target_type: file + display_field: false + display_default: false + uri_scheme: public +module: image +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/core/profiles/standard/config/optional/media.type.image.yml b/core/profiles/standard/config/optional/media.type.image.yml new file mode 100644 index 0000000..a823e03 --- /dev/null +++ b/core/profiles/standard/config/optional/media.type.image.yml @@ -0,0 +1,14 @@ +langcode: en +status: true +dependencies: + module: + - media +id: image +label: Image +description: 'Use the "Image" media type for uploading local images.' +handler: image +queue_thumbnail_downloads: false +new_revision: false +handler_configuration: + source_field: field_media_image +field_map: { }