class FileImageDimensionsConstraintValidator

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

Validator for the FileImageDimensionsConstraint.

This validator will resize the image if exceeds the limits.

Hierarchy

Expanded class hierarchy of FileImageDimensionsConstraintValidator

File

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

Namespace

Drupal\file\Plugin\Validation\Constraint
View source
class FileImageDimensionsConstraintValidator extends BaseFileConstraintValidator implements ContainerInjectionInterface {
    use StringTranslationTrait;
    
    /**
     * Creates a new FileImageDimensionsConstraintValidator.
     *
     * @param \Drupal\Core\Image\ImageFactory $imageFactory
     *   The image factory.
     * @param \Drupal\Core\Messenger\MessengerInterface $messenger
     *   The messenger.
     */
    public function __construct(ImageFactory $imageFactory, MessengerInterface $messenger) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('image.factory'), $container->get('messenger'));
    }
    
    /**
     * {@inheritdoc}
     */
    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(),
                ]);
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BaseFileConstraintValidator::assertValueIsFile protected function Checks the value is of type FileInterface.
FileImageDimensionsConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FileImageDimensionsConstraintValidator::validate public function
FileImageDimensionsConstraintValidator::__construct public function Creates a new FileImageDimensionsConstraintValidator.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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