function FileIsImageConstraintValidator::validate

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

File

core/modules/file/src/Plugin/Validation/Constraint/FileIsImageConstraintValidator.php, line 38

Class

FileIsImageConstraintValidator
Validator for the FileIsImageConstraint.

Namespace

Drupal\file\Plugin\Validation\Constraint

Code

public function validate(mixed $value, Constraint $constraint) {
    $file = $this->assertValueIsFile($value);
    if (!$constraint instanceof FileIsImageConstraint) {
        throw new UnexpectedTypeException($constraint, FileIsImageConstraint::class);
    }
    $image = $this->imageFactory
        ->get($file->getFileUri());
    if (!$image->isValid()) {
        $supportedExtensions = $this->imageFactory
            ->getSupportedExtensions();
        $this->context
            ->addViolation($constraint->message, [
            '%types' => implode(', ', $supportedExtensions),
        ]);
    }
}

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