W3cubDocs

/Drupal 8

function 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();
          $file->filesize = $image->getFileSize();
          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 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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.module/function/file_validate_image_resolution/8.1.x