file_validate_image_resolution

file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0)

Verifies that image dimensions are within the specified maximum and minimum.

Non-image files will be ignored. If an image toolkit is available the image will be scaled to fit within the desired maximum dimensions.

Parameters

\Drupal\file\FileInterface $file: A file entity. This function may resize the file affecting its size.

string|int $maximum_dimensions: (optional) A string in the form WIDTHxHEIGHT; for example, '640x480' or '85x85'. If an image toolkit is installed, the image will be resized down to these dimensions. A value of zero (the default) indicates no restriction on size, so no resizing will be attempted.

string|int $minimum_dimensions: (optional) A string in the form WIDTHxHEIGHT. This will check that the image meets a minimum size. A value of zero (the default) indicates that there is no restriction on size.

Return value

array An empty array if the file meets the specified dimensions, was resized successfully to meet those requirements or is not an image. If the image does not meet the requirements or an attempt to resize it fails, an array containing the error message will be returned.

See also

hook_file_validate()

File

core/modules/file/file.module, line 431
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

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

  // 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.', array('%dimensions' => $maximum_dimensions));
          }
          elseif (empty($width)) {
            $message = t('The image was resized to fit within the maximum allowed height of %height pixels.', array('%height' => $height));
          }
          elseif (empty($height)) {
            $message = t('The image was resized to fit within the maximum allowed width of %width pixels.', array('%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.', array('%dimensions' => $minimum_dimensions));
      }
    }
  }

  return $errors;
}
doc_Drupal
2016-10-29 09:14:19
Comments
Leave a Comment

Please login to continue.