From d2831d2a046583e8d71ffb47df7536b82ccd09c7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Joshua=20R=C3=BCsweg?= Date: Fri, 27 Sep 2019 20:04:35 +0200 Subject: [PATCH] Add minimum/maximum image width for UploadFormField --- .../builder/field/UploadFormField.class.php | 131 ++++++++++++++++++ wcfsetup/install/lang/de.xml | 2 + wcfsetup/install/lang/en.xml | 2 + 3 files changed, 135 insertions(+) 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 b2becacbdc..f9a923a5e6 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 @@ -48,6 +48,18 @@ class UploadFormField extends AbstractFormField { */ protected $maximumFilesize; + /** + * minimum image width for each uploaded file + * @var null|number + */ + protected $minimumImageWidth; + + /** + * maximum image width for each uploaded file + * @var null|number + */ + protected $maximumImageWidth; + /** * @inheritDoc */ @@ -175,6 +187,33 @@ class UploadFormField extends AbstractFormField { } } } + + if ($this->getMinimumImageWidth() !== null || $this->getMaximumImageWidth() !== null) { + foreach ($this->getValue() as $file) { + $imagesize = getimagesize($file->getLocation()); + + if ($this->getMinimumImageWidth() !== null && $this->getMinimumImageWidth() >= $imagesize[0]) { + $this->addValidationError(new FormFieldValidationError( + 'minimumImageWidth', + 'wcf.form.field.upload.error.minimumImageWidth', + [ + 'minimumImageWidth' => $this->getMinimumImageWidth(), + 'file' => $file + ] + )); + } + else if ($this->getMaximumImageWidth() !== null && $imagesize[0] > $this->getMaximumImageWidth()) { + $this->addValidationError(new FormFieldValidationError( + 'maximumImageWidth', + 'wcf.form.field.upload.error.maximumImageWidth', + [ + 'maximumImageWidth' => $this->getMaximumImageWidth(), + 'file' => $file + ] + )); + } + } + } } /** @@ -335,14 +374,106 @@ class UploadFormField extends AbstractFormField { return $this->maximumFilesize; } + /** + * Sets the minimum image width for each uploaded file. If `null` is passed, the + * minimum image width is removed. + * + * @param null|number $minimumImageWidth the mimimum image width + * @return static this field + * + * @throws \InvalidArgumentException if the given mimimum image width is no number or otherwise invalid + * @throws \LogicException if the form field is not marked as image only + */ + public function minimumImageWidth($minimumImageWidth = null) { + if (!$this->isImageOnly()) { + throw new \LogicException("The form field must be image only, to set a minimum image width."); + } + + if ($minimumImageWidth !== null) { + if (!is_numeric($minimumImageWidth)) { + throw new \InvalidArgumentException("Given minimum image width is no int, '" . gettype($minimumImageWidth) . "' given."); + } + + $maximumImageWidth = $this->getMaximumImageWidth(); + if ($maximumImageWidth !== null && $minimumImageWidth > $maximumImageWidth) { + throw new \InvalidArgumentException("Minimum image width ({$minimumImageWidth}) cannot be greater than maximum image width ({$maximumImageWidth})."); + } + } + + $this->minimumImageWidth = $minimumImageWidth; + + return $this; + } + + /** + * Returns the mimimum image width of each file or `null` if no mimimum image width + * has been set. + * + * @return null|number + */ + public function getMinimumImageWidth() { + return $this->minimumImageWidth; + } + + /** + * Sets the maximum image width for each uploaded file. If `null` is passed, the + * maximum image width is removed. + * + * @param null|number $maximumImageWidth the maximum image width + * @return static this field + * + * @throws \InvalidArgumentException if the given mimimum image width is no number or otherwise invalid + * @throws \LogicException if the form field is not marked as image only + */ + public function maximumImageWidth($maximumImageWidth = null) { + if (!$this->isImageOnly()) { + throw new \LogicException("The form field must be image only, to set a maximum image width."); + } + + if ($maximumImageWidth !== null) { + if (!is_numeric($maximumImageWidth)) { + throw new \InvalidArgumentException("Given maximum image width is no int, '" . gettype($maximumImageWidth) . "' given."); + } + + $minimumImageWidth = $this->getMinimumImageWidth(); + if ($maximumImageWidth !== null && $minimumImageWidth > $maximumImageWidth) { + throw new \InvalidArgumentException("Maximum image width ({$maximumImageWidth}) cannot be smaller than minimum image width ({$minimumImageWidth})."); + } + } + + $this->maximumImageWidth = $maximumImageWidth; + + return $this; + } + + /** + * Returns the maximum image width of each file or `null` if no maximum image width + * has been set. + * + * @return null|number + */ + public function getMaximumImageWidth() { + return $this->maximumImageWidth; + } + /** * Sets the flag for `imageOnly`. This flag indicates whether only images * can uploaded via this field. Other file types will be rejected during upload. * * @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 */ 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."); + } + $this->imageOnly = $imageOnly; return $this; diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index b3e7006670..b41807fd6a 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -3928,6 +3928,8 @@ Dateianhänge: getFilename()}“ darf maximal {$maximumFilesize|filesize} groß sein.]]> + getFilename()}“ muss mindestens {#$minimumImageWidth} Pixel breit sein.]]> + getFilename()}“ darf maximal {#$maximumImageWidth} Pixel breit sein.]]> diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 8081c1c6ea..012464c39b 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -3874,6 +3874,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.]]> -- 2.20.1