diff --git a/core/modules/file/file.module b/core/modules/file/file.module index c63fff4..4a93569 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -14,6 +14,7 @@ use Drupal\Core\Url; use Drupal\file\Entity\File; use Drupal\file\FileInterface; +use Drupal\image\Entity\ImageStyle; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityStorageInterface; @@ -472,6 +473,44 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions } /** + * Verifies that the uploaded image is processed with an image style. + * + * @param \Drupal\file\FileInterface $file + * A file entity. This function may resize the file affecting its size. + * @param string $image_style_name + * The name of the image style to use to process the image. + * + * @return array + * An empty array if the image style was not specified, or the image file was + * processed successfully. An array containing the error messages otherwise. + * + * @see hook_file_validate() + */ +function file_validate_image_style_on_upload(FileInterface $file, $image_style_name) { + $errors = []; + + if (!empty($image_style_name)) { + $image_style = ImageStyle::load($image_style_name); + if ($image_style === NULL) { + $errors[] = t("The image style %image_style to be applied on the uploaded image does not exist.", ['%image_style' => $image_style_name]); + } + else { + $derived_uri = $file->getFileUri() . '.derived'; + if (!$image_style->createDerivative($file->getFileUri(), $derived_uri)) { + $errors[] = t('The uploaded image could not be processed with style %image_style. The image file may be invalid.', ['%image_style' => $image_style_name]); + } + else { + if (file_unmanaged_move($derived_uri, $file->getFileUri(), FILE_EXISTS_REPLACE) === FALSE) { + $errors[] = t('An error occurred while saving the uploaded image file.'); + } + } + } + } + + return $errors; +} + +/** * Saves a file to the specified destination and creates a database entry. * * @param string $data diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml index 0006ad7..f3ff54b 100644 --- a/core/modules/image/config/schema/image.schema.yml +++ b/core/modules/image/config/schema/image.schema.yml @@ -102,6 +102,9 @@ field.field_settings.image: type: base_file_field_field_settings label: 'Image settings' mapping: + image_style: + type: string + label: 'Image style on upload' max_resolution: type: string label: 'Maximum image resolution' diff --git a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php index c83e143..c992c56 100644 --- a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php +++ b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php @@ -76,6 +76,7 @@ public static function defaultFieldSettings() { 'alt_field_required' => 1, 'title_field' => 0, 'title_field_required' => 0, + 'image_style' => '', 'max_resolution' => '', 'min_resolution' => '', 'default_image' => array( @@ -199,13 +200,24 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); + // Add image style on upload. + $element['image_style'] = [ + '#type' => 'select', + '#title' => t('Image style on upload'), + '#description' => t('Select if an image style should be applied to the uploaded image file.'), + '#options' => image_style_options(), + '#default_value' => $settings['image_style'], + '#empty_option' => $this->t('- No -'), + '#weight' => 4.1, + ]; + // Add maximum and minimum resolution settings. $max_resolution = explode('x', $settings['max_resolution']) + array('', ''); $element['max_resolution'] = array( '#type' => 'item', '#title' => t('Maximum image resolution'), '#element_validate' => array(array(get_class($this), 'validateResolution')), - '#weight' => 4.1, + '#weight' => 4.2, '#field_prefix' => '