Image fields minimum dimension restrictions don't apply equally in D7 and D8

Created on 11 November 2017, about 7 years ago
Updated 5 December 2023, 12 months ago

Problem/Motivation

Drupal 8 has a different behavior if compared to Drupal 7 when it comes to image fields minimum dimension restrictions.

In Drupal 7, images were scaled down according to the maximum dimensions, and the resulting image was compared against the minimum dimension restrictions:

function file_validate_image_resolution(stdClass $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
  $errors = array();

  // Check first that the file is an image.
  if ($info = image_get_info($file->uri)) {
    if ($maximum_dimensions) {
      // Check that it is smaller than the given dimensions.
      list($width, $height) = explode('x', $maximum_dimensions);
      if ($info['width'] > $width || $info['height'] > $height) {
        // Try to resize the image to fit the dimensions.
        if ($image = image_load($file->uri)) {
          image_scale($image, $width, $height);
          image_save($image);
          $file->filesize = $image->info['file_size'];
          drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)));
        }
        else {
          $errors[] = t('The image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => $maximum_dimensions));
        }
      }
    }

    if ($minimum_dimensions) {
      // Check that it is larger than the given dimensions.
      list($width, $height) = explode('x', $minimum_dimensions);
      if ($info['width'] < $width || $info['height'] < $height) {
        $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => $minimum_dimensions));
      }
    }
  }

  return $errors;
}

In Drupal 8, the image is scaled down as well, but the original file is compared against the minimum dimension restrictions:

function file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
  $errors = [];

  // Check first that the file is an image.
  $image_factory = \Drupal::service('image.factory');
  $image = $image_factory->get($file->getFileUri());
  if ($image->isValid()) {
    if ($maximum_dimensions) {
      // Check that it is smaller than the given dimensions.
      list($width, $height) = explode('x', $maximum_dimensions);
      if ($image->getWidth() > $width || $image->getHeight() > $height) {
        // Try to resize the image to fit the dimensions.
        if ($image->scale($width, $height)) {
          $image->save();
          if (!empty($width) && !empty($height)) {
            $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $maximum_dimensions]);
          }
          elseif (empty($width)) {
            $message = t('The image was resized to fit within the maximum allowed height of %height pixels.', ['%height' => $height]);
          }
          elseif (empty($height)) {
            $message = t('The image was resized to fit within the maximum allowed width of %width pixels.', ['%width' => $width]);
          }
          drupal_set_message($message);
        }
        else {
          $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.');
        }
      }
    }

    if ($minimum_dimensions) {
      // Check that it is larger than the given dimensions.
      list($width, $height) = explode('x', $minimum_dimensions);
      if ($image->getWidth() < $width || $image->getHeight() < $height) {
        $errors[] = t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => $minimum_dimensions]);
      }
    }
  }

  return $errors;
}

This seems to be a D7 bug, but as the original bug report (see below, and following comments) indicates, some people were relying on that behavior to manage image restrictions, and may expect the same behavior in Drupal 8.

Proposed resolution

- Decide if this is a D7 bug or a D8 bug
- Modify the image handling to make D7 and D8 behave the same way

Remaining tasks

- Decide if this is a "bug or feature"
- Review patches
-

User interface changes

API changes

Data model changes

--- Original bug report from @drupi17 ----

Hi,

On image upload, if there is a min and max resolution set to, for example, 480x480 then the following error message is displayed. The expectation is that any image smaller than 480x480 is rejected, but anything larger is resized down to 480x480. The standard image field does not allow for upload if these settings are set. This was working just fine in Drupal 7, but is now broken in Drupal 8. This is a crucial part of validation and responsive design to have the system resize the images. Need a solution for this.

Error message (both display when uploading an image that is >= 480x480)

The image was resized to fit within the maximum allowed dimensions of 480x480 pixels.

The image is too small; the minimum dimensions are 480x480 pixels.

🐛 Bug report
Status

Fixed

Version

7.0 ⚰️

Component
Image system 

Last updated 2 days ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024