I may be wrong but I think the default file_validate_image_resolution() function is not only doing validation but is also manipulating the uploaded image:

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));
        }
      }

In some situation, we only want to check the image resolution fit some requirements without any scale effect.

My suggestion is to add a new file_validate_image_resolution function which only focus on validating the requirements.

Comments

vbouchet created an issue. See original summary.

azzida’s picture

I found a functional workaround. We had a hard requirement that a particular image field be restricted to '1564x680' only. Moreso, they wanted to prevent resizing if uploading an image that was too big.
This workaround tested in Drupal 7.51 with Media module 7.x-2.0-beta5.

function MODULE_form_alter(&$form, &$form_state, $form_id){
//Add the custom validator to reject invalid resolutions
  if ($form_id == 'file_entity_add_upload') {
    if (isset($form['#options']['min_resolution'])){
      $min = $form['#options']['min_resolution'];
    } else { $min = '1564x680'; }

    if (isset($form['#options']['max_resolution'])){
      $max = $form['#options']['max_resolution'];
    } else { $max = '1564x680'; }

    $form['upload']['#upload_validators']['mycustom_image_validator'][] = $min;
    $form['upload']['#upload_validators']['mycustom_image_validator'][] = $max;
    unset($form['upload']['#upload_validators']['file_validate_image_resolution']);
  }
}

function mycustom_image_validator($field, $maximum_dimensions = 0, $minimum_dimensions = 0) {
$errors = array();
  // Check first that the file is an image.
  if ($info = image_get_info($field->uri)) {
    if ($info = image_get_info($field->uri)) {
    if ($maximum_dimensions) {
      // Check that it is smaller or equal to the given dimensions.
      list($width, $height) = explode('x', $maximum_dimensions);
      if ($info['width'] > $width) {
        $errors[] = t('The image width is too big (%widthpx); please upload an image that is exacty %dimensions pixels.',
          array('%dimensions' => $maximum_dimensions, 
            '%width' => $info['width']));
      }
      else if ($info['height'] > $height) {
        $errors[] = t('The image height is too big (%heightpx); please upload an image that is exacty %dimensions pixels.',
          array('%dimensions' => $maximum_dimensions, 
            '%height' => $info['height']));
      }
    }

    if ($minimum_dimensions) {
      // Check that it is larger than the given dimensions.
      list($width, $height) = explode('x', $minimum_dimensions);
      if ($info['width'] < $width) {
        $errors[] = t('The image width is too small (%widthpx); please upload an image that is exacty %dimensions pixels.',
          array('%dimensions' => $maximum_dimensions, 
            '%width' => $info['width']));
      }
      else if ($info['height'] < $height) {
        $errors[] = t('The image height is too small (%heightpx); please upload an image that is exacty %dimensions pixels.',
          array('%dimensions' => $maximum_dimensions, 
            '%height' => $info['height']));
      }
    }
  }
  else {
    $errors[] = t('File is not a valid image.');
  }

  return $errors;
}
Daniel Ward’s picture

Could you do this using hook_entity_form_display_alter()? That would allow you to access the field definitions for your entity type and set some additional resolution settings on the image field. Those settings are enforced by the image widget so you wouldn't need any custom validation. Something like this perhaps:

function MODULE_entity_form_display_alter(\Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display, array $context) {
  $fields = \Drupal::service('entity_field.manager')->getFieldDefinitions($context['entity_type'], $context['bundle']);
  //Set additional settings on the image field to force max and min resolutions
  $fields['image']->setSettings(['max_resolution' => '1564x680']);
  $fields['image']->setSettings(['min_resolution' => '1564x680']);
}
apaderno’s picture

Status: Active » Closed (outdated)

I am closing this issue, since the code shown in the IS isn't used from this module.