You are here

function file_validate_image_resolution in Drupal 8

Same name and namespace in other branches
  1. 6 includes/file.inc \file_validate_image_resolution()
  2. 7 includes/file.inc \file_validate_image_resolution()
  3. 9 core/modules/file/file.module \file_validate_image_resolution()

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()

1 call to file_validate_image_resolution()
ValidatorTest::testFileValidateImageResolution in core/modules/file/tests/src/Kernel/ValidatorTest.php
This ensures the resolution of a specific file is within bounds.

File

core/modules/file/file.module, line 478
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 = [];

  // Check first that the file is an image.
  $image_factory = \Drupal::service('image.factory');
  $image = $image_factory
    ->get($file
    ->getFileUri());
  if ($image
    ->isValid()) {
    $scaling = FALSE;
    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)) {
          $scaling = TRUE;
          $image
            ->save();
          if (!empty($width) && !empty($height)) {
            $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.', [
              '%dimensions' => $maximum_dimensions,
              '%new_width' => $image
                ->getWidth(),
              '%new_height' => $image
                ->getHeight(),
            ]);
          }
          elseif (empty($width)) {
            $message = t('The image was resized to fit within the maximum allowed height of %height pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.', [
              '%height' => $height,
              '%new_width' => $image
                ->getWidth(),
              '%new_height' => $image
                ->getHeight(),
            ]);
          }
          elseif (empty($height)) {
            $message = t('The image was resized to fit within the maximum allowed width of %width pixels. The new dimensions of the resized image are %new_widthx%new_height pixels.', [
              '%width' => $width,
              '%new_width' => $image
                ->getWidth(),
              '%new_height' => $image
                ->getHeight(),
            ]);
          }
          \Drupal::messenger()
            ->addStatus($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) {
        if ($scaling) {
          $errors[] = t('The resized image is too small. The minimum dimensions are %dimensions pixels and after resizing, the image size will be %widthx%height pixels.', [
            '%dimensions' => $minimum_dimensions,
            '%width' => $image
              ->getWidth(),
            '%height' => $image
              ->getHeight(),
          ]);
        }
        else {
          $errors[] = t('The image is too small. The minimum dimensions are %dimensions pixels and the image size is %widthx%height pixels.', [
            '%dimensions' => $minimum_dimensions,
            '%width' => $image
              ->getWidth(),
            '%height' => $image
              ->getHeight(),
          ]);
        }
      }
    }
  }
  return $errors;
}