[null, 'The exact width of the image', 'int'], 'height' => [null, 'The exact height of the image', 'int'], 'minWidth' => [0, 'The minimum width of the image', 'int'], 'maxWidth' => [PHP_INT_MAX, 'The maximum width of the image', 'int'], 'minHeight' => [0, 'The minimum height of the image', 'int'], 'maxHeight' => [PHP_INT_MAX, 'The maximum heigt of the image', 'int'], 'heightMessage' => [null, 'Translation key or message for invalid height', 'string'], 'widthMessage' => [null, 'Translation key or message for invalid width', 'string'], 'minWidthMessage' => [null, 'Translation key or message for invalid minimum width', 'string'], 'maxWidthMessage' => [null, 'Translation key or message for invalid maximum width', 'string'], 'minHeightMessage' => [null, 'Translation key or message for invalid minimum height', 'string'], 'maxHeightMessage' => [null, 'Translation key or message for invalid maximum height', 'string'], ]; public function isValid(mixed $value): void { $this->ensureFileUploadTypes($value); $this->validateOptions(); if ($value instanceof UploadedFile) { $this->validateUploadedFile($value); } elseif ($value instanceof ObjectStorage && $value->count() > 0) { $index = 0; foreach ($value as $uploadedFile) { $this->validateUploadedFile($uploadedFile, $index); $index++; } } } private function validateUploadedFile(UploadedFile $uploadedFile, ?int $index = null): void { $imageInfo = $this->getImageInfo($uploadedFile->getTemporaryFileName()); if ($imageInfo->getWidth() === 0 || $imageInfo->getHeight() === 0) { // Silently ignore files, where the width or height could not be determined. Most likely no image file. return; } if (isset($this->options['width']) && (int)$this->options['width'] !== $imageInfo->getWidth()) { $message = $this->translateErrorMessage( $this->widthMessage, '', [$this->options['width']] ); $code = 1715964040; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } if (isset($this->options['height']) && (int)$this->options['height'] !== $imageInfo->getHeight()) { $message = $this->translateErrorMessage( $this->heightMessage, '', [$this->options['height']] ); $code = 1715964041; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } if ((int)$this->options['minWidth'] > $imageInfo->getWidth()) { $message = $this->translateErrorMessage( $this->minWidthMessage, '', [$this->options['minWidth']] ); $code = 1715964042; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } if ((int)$this->options['minHeight'] > $imageInfo->getHeight()) { $message = $this->translateErrorMessage( $this->minHeightMessage, '', [$this->options['minHeight']] ); $code = 1715964043; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } if ((int)$this->options['maxWidth'] < $imageInfo->getWidth()) { $message = $this->translateErrorMessage( $this->maxWidthMessage, '', [$this->options['maxWidth']] ); $code = 1715964044; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } if ((int)$this->options['maxHeight'] < $imageInfo->getHeight()) { $message = $this->translateErrorMessage( $this->maxHeightMessage, '', [$this->options['maxHeight']] ); $code = 1715964045; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } } private function getImageInfo(string $filePath): ImageInfo { return GeneralUtility::makeInstance(ImageInfo::class, $filePath); } /** * Checks if this validator is correctly configured */ private function validateOptions(): void { if ((int)$this->options['minWidth'] > (int)$this->options['maxWidth']) { throw new InvalidValidationOptionsException('The option "minWidth" must not be greater than "maxWidth"', 1716008127); } if ((int)$this->options['minHeight'] > (int)$this->options['maxHeight']) { throw new InvalidValidationOptionsException('The option "minHeight" must not be greater than "maxHeight"', 1716008128); } } }