function file_validate_image_resolution

Same name in other branches
  1. 7.x includes/file.inc \file_validate_image_resolution()
  2. 9 core/modules/file/file.module \file_validate_image_resolution()
  3. 8.9.x 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.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use the 'file.validator' service instead.

See also

https://www.drupal.org/node/3363700

hook_file_validate()

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

File

core/modules/file/file.module, line 276

Code

function file_validate_image_resolution(FileInterface $file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
    @trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use the \'file.validator\' service instead. See https://www.drupal.org/node/3363700', E_USER_DEPRECATED);
    $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.
            [
                $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();
                    // Update the file size now that the image has been resized.
                    $file->setSize($image->getFileSize());
                    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.
            [
                $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.