function FileImageDimensionsConstraintValidator::validate

Same name in other branches
  1. 10 core/modules/file/src/Plugin/Validation/Constraint/FileImageDimensionsConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileImageDimensionsConstraintValidator::validate()

File

core/modules/file/src/Plugin/Validation/Constraint/FileImageDimensionsConstraintValidator.php, line 48

Class

FileImageDimensionsConstraintValidator
Validator for the FileImageDimensionsConstraint.

Namespace

Drupal\file\Plugin\Validation\Constraint

Code

public function validate(mixed $value, Constraint $constraint) : void {
    $file = $this->assertValueIsFile($value);
    if (!$constraint instanceof FileImageDimensionsConstraint) {
        throw new UnexpectedTypeException($constraint, FileImageDimensionsConstraint::class);
    }
    $image = $this->imageFactory
        ->get($file->getFileUri());
    if (!$image->isValid()) {
        return;
    }
    $scaling = FALSE;
    $maxDimensions = $constraint->maxDimensions;
    if ($maxDimensions) {
        // Check that it is smaller than the given dimensions.
        [
            $width,
            $height,
        ] = explode('x', $maxDimensions);
        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)) {
                    $this->messenger
                        ->addStatus($this->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' => $maxDimensions,
                        '%new_width' => $image->getWidth(),
                        '%new_height' => $image->getHeight(),
                    ]));
                }
                elseif (empty($width)) {
                    $this->messenger
                        ->addStatus($this->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)) {
                    $this->messenger
                        ->addStatus($this->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(),
                    ]));
                }
            }
            else {
                $this->context
                    ->addViolation($constraint->messageResizeFailed);
            }
        }
    }
    $minDimensions = $constraint->minDimensions;
    if ($minDimensions) {
        // Check that it is larger than the given dimensions.
        [
            $width,
            $height,
        ] = explode('x', $minDimensions);
        if ($image->getWidth() < $width || $image->getHeight() < $height) {
            if ($scaling) {
                $this->context
                    ->addViolation($constraint->messageResizedImageTooSmall, [
                    '%dimensions' => $minDimensions,
                    '%width' => $image->getWidth(),
                    '%height' => $image->getHeight(),
                ]);
                return;
            }
            $this->context
                ->addViolation($constraint->messageImageTooSmall, [
                '%dimensions' => $minDimensions,
                '%width' => $image->getWidth(),
                '%height' => $image->getHeight(),
            ]);
        }
    }
}

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