From 09550e789523a59687a029c620835da3b0956aa6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Fri, 27 Sep 2019 20:28:39 +0200 Subject: [PATCH] Add minimum/maximum image height for UploadFormField --- .../builder/field/UploadFormField.class.php | 143 ++++++++++++++++-- wcfsetup/install/lang/de.xml | 2 + wcfsetup/install/lang/en.xml | 2 + 3 files changed, 138 insertions(+), 9 deletions(-) diff --git a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php index f9a923a5e6..d49101fcd0 100644 --- a/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php +++ b/wcfsetup/install/files/lib/system/form/builder/field/UploadFormField.class.php @@ -60,6 +60,18 @@ class UploadFormField extends AbstractFormField { */ protected $maximumImageWidth; + /** + * minimum image height for each uploaded file + * @var null|number + */ + protected $minimumImageHeight; + + /** + * maximum image height for each uploaded file + * @var null|number + */ + protected $maximumImageHeight; + /** * @inheritDoc */ @@ -188,11 +200,11 @@ class UploadFormField extends AbstractFormField { } } - if ($this->getMinimumImageWidth() !== null || $this->getMaximumImageWidth() !== null) { + if ($this->getMinimumImageWidth() !== null || $this->getMaximumImageWidth() !== null || $this->getMinimumImageHeight() !== null || $this->getMaximumImageHeight() !== null) { foreach ($this->getValue() as $file) { $imagesize = getimagesize($file->getLocation()); - if ($this->getMinimumImageWidth() !== null && $this->getMinimumImageWidth() >= $imagesize[0]) { + if ($this->getMinimumImageWidth() !== null && $this->getMinimumImageWidth() > $imagesize[0]) { $this->addValidationError(new FormFieldValidationError( 'minimumImageWidth', 'wcf.form.field.upload.error.minimumImageWidth', @@ -212,6 +224,27 @@ class UploadFormField extends AbstractFormField { ] )); } + + if ($this->getMinimumImageHeight() !== null && $this->getMinimumImageHeight() > $imagesize[1]) { + $this->addValidationError(new FormFieldValidationError( + 'minimumImageHeight', + 'wcf.form.field.upload.error.minimumImageHeight', + [ + 'minimumImageHeight' => $this->getMinimumImageHeight(), + 'file' => $file + ] + )); + } + else if ($this->getMaximumImageHeight() !== null && $imagesize[0] > $this->getMaximumImageHeight()) { + $this->addValidationError(new FormFieldValidationError( + 'maximumImageHeight', + 'wcf.form.field.upload.error.maximumImageHeight', + [ + 'maximumImageHeight' => $this->getMaximumImageHeight(), + 'file' => $file + ] + )); + } } } } @@ -456,6 +489,88 @@ class UploadFormField extends AbstractFormField { return $this->maximumImageWidth; } + /** + * Sets the minimum image height for each uploaded file. If `null` is passed, the + * minimum image height is removed. + * + * @param null|number $minimumImageHeight the mimimum image height + * @return static this field + * + * @throws \InvalidArgumentException if the given mimimum image height is no number or otherwise invalid + * @throws \LogicException if the form field is not marked as image only + */ + public function minimumImageHeight($minimumImageHeight = null) { + if (!$this->isImageOnly()) { + throw new \LogicException("The form field must be image only, to set a minimum image height."); + } + + if ($minimumImageHeight !== null) { + if (!is_numeric($minimumImageHeight)) { + throw new \InvalidArgumentException("Given minimum image height is no int, '" . gettype($minimumImageHeight) . "' given."); + } + + $maximumImageHeight = $this->getMaximumImageHeight(); + if ($maximumImageHeight !== null && $minimumImageHeight > $maximumImageHeight) { + throw new \InvalidArgumentException("Minimum image height ({$minimumImageHeight}) cannot be greater than maximum image height ({$maximumImageHeight})."); + } + } + + $this->minimumImageHeight = $minimumImageHeight; + + return $this; + } + + /** + * Returns the mimimum image height of each file or `null` if no mimimum image height + * has been set. + * + * @return null|number + */ + public function getMinimumImageHeight() { + return $this->minimumImageHeight; + } + + /** + * Sets the maximum image height for each uploaded file. If `null` is passed, the + * maximum image height is removed. + * + * @param null|number $maximumImageHeight the maximum image height + * @return static this field + * + * @throws \InvalidArgumentException if the given mimimum image height is no number or otherwise invalid + * @throws \LogicException if the form field is not marked as image only + */ + public function maximumImageHeight($maximumImageHeight = null) { + if (!$this->isImageOnly()) { + throw new \LogicException("The form field must be image only, to set a maximum image height."); + } + + if ($maximumImageHeight !== null) { + if (!is_numeric($maximumImageHeight)) { + throw new \InvalidArgumentException("Given maximum image height is no int, '" . gettype($maximumImageHeight) . "' given."); + } + + $minimumImageHeight = $this->getMinimumImageHeight(); + if ($minimumImageHeight !== null && $maximumImageHeight > $minimumImageHeight) { + throw new \InvalidArgumentException("Maximum image height ({$maximumImageHeight}) cannot be smaller than minimum image height ({$minimumImageHeight})."); + } + } + + $this->maximumImageWidth = $maximumImageHeight; + + return $this; + } + + /** + * Returns the maximum image height of each file or `null` if no maximum image height + * has been set. + * + * @return null|number + */ + public function getMaximumImageHeight() { + return $this->maximumImageHeight; + } + /** * Sets the flag for `imageOnly`. This flag indicates whether only images * can uploaded via this field. Other file types will be rejected during upload. @@ -463,15 +578,25 @@ class UploadFormField extends AbstractFormField { * @param boolean $imageOnly * @return static this field * - * @throws \InvalidArgumentException if the field is not set to images only and a minimum/maximum width is set + * @throws \InvalidArgumentException if the field is not set to images only and a minimum/maximum width/height is set */ public function imageOnly($imageOnly = true) { - if ($imageOnly && $this->getMinimumImageWidth() !== null) { - throw new \InvalidArgumentException("The form field must be image only, because a minimum image width is set."); - } - - if ($imageOnly && $this->getMaximumImageWidth() !== null) { - throw new \InvalidArgumentException("The form field must be image only, because a maximum image width is set."); + if (!$imageOnly) { + if ($this->getMinimumImageWidth() !== null) { + throw new \InvalidArgumentException("The form field must be image only, because a minimum image width is set."); + } + + if ($this->getMaximumImageWidth() !== null) { + throw new \InvalidArgumentException("The form field must be image only, because a maximum image width is set."); + } + + if ($this->getMinimumImageHeight() !== null) { + throw new \InvalidArgumentException("The form field must be image only, because a minimum image height is set."); + } + + if ($this->getMaximumImageHeight() !== null) { + throw new \InvalidArgumentException("The form field must be image only, because a maximum image height is set."); + } } $this->imageOnly = $imageOnly; diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index b41807fd6a..5931643a00 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3930,6 +3930,8 @@ Dateianhänge: getFilename()}“ darf maximal {$maximumFilesize|filesize} groß sein.]]> getFilename()}“ muss mindestens {#$minimumImageWidth} Pixel breit sein.]]> getFilename()}“ darf maximal {#$maximumImageWidth} Pixel breit sein.]]> + getFilename()}“ muss mindestens {#$minimumImageHeight} Pixel hoch sein.]]> + getFilename()}“ darf maximal {#$maximumImageHeight} Pixel hoch sein.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 012464c39b..787591d8cb 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3876,6 +3876,8 @@ Attachments: getFilename()}” may have a maximum size of {$maximumFilesize|filesize}.]]> getFilename()}“ may have a minimum width of {#$minimumImageWidth} pixels.]]> getFilename()}” may have a maximum width of {#$maximumImageWidth} pixels.]]> + getFilename()}“ may have a minimum height of {#$minimumImageHeight} pixels.]]> + getFilename()}” may have a maximum height of {#$maximumImageHeight} pixels.]]> -- 2.20.1