From 80598e6b65fb64899a2edb2d7641b813a298795d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 24 Jul 2020 12:20:13 +0200 Subject: [PATCH] Expand style upload field definitions to store image size constraints --- .../install/files/acp/templates/styleAdd.tpl | 20 ++ .../files/lib/acp/form/StyleAddForm.class.php | 217 +++++++----------- 2 files changed, 108 insertions(+), 129 deletions(-) diff --git a/wcfsetup/install/files/acp/templates/styleAdd.tpl b/wcfsetup/install/files/acp/templates/styleAdd.tpl index 80b73c7e6b..58e682c54b 100644 --- a/wcfsetup/install/files/acp/templates/styleAdd.tpl +++ b/wcfsetup/install/files/acp/templates/styleAdd.tpl @@ -238,6 +238,15 @@
{@$__wcf->getUploadHandler()->renderField('image')} + {if $errorField == 'image'} + + {if $errorType == 'empty'} + {lang}wcf.global.form.error.empty{/lang} + {else} + {lang}wcf.acp.style.image.error.{$errorType}{/lang} + {/if} + + {/if} {lang}wcf.acp.style.image.description{/lang}
@@ -245,6 +254,15 @@
{@$__wcf->getUploadHandler()->renderField('image2x')} + {if $errorField == 'image2x'} + + {if $errorType == 'empty'} + {lang}wcf.global.form.error.empty{/lang} + {else} + {lang}wcf.acp.style.image2x.error.{$errorType}{/lang} + {/if} + + {/if} {lang}wcf.acp.style.image2x.description{/lang}
@@ -283,6 +301,8 @@ {if $errorType == 'empty'} {lang}wcf.global.form.error.empty{/lang} + {elseif $errorType == 'minWidth' || $errorType == 'minHeight' || $errorType == 'maxWidth' || $errorType == 'maxHeight'} + {lang}wcf.image.coverPhoto.upload.error.dimensions{/lang} {else} {lang}wcf.acp.style.favicon.error.{$errorType}{/lang} {/if} diff --git a/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php b/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php index 87ef89fc5b..d29a6a7327 100644 --- a/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/StyleAddForm.class.php @@ -242,12 +242,41 @@ class StyleAddForm extends AbstractForm { */ protected function getUploadFields() { return [ - 'image' => [], - 'image2x' => [], - 'pageLogo' => ['allowSvgImage' => true], - 'pageLogoMobile' => ['allowSvgImage' => true], - 'coverPhoto' => [], - 'favicon' => [], + 'image' => [ + 'size' => [ + 'maxWidth' => Style::PREVIEW_IMAGE_MAX_WIDTH, + 'maxHeight' => Style::PREVIEW_IMAGE_MAX_WIDTH, + 'preserveAspectRatio' => false, + ], + ], + 'image2x' => [ + 'size' => [ + 'maxWidth' => 2 * Style::PREVIEW_IMAGE_MAX_WIDTH, + 'maxHeight' => 2 * Style::PREVIEW_IMAGE_MAX_WIDTH, + 'preserveAspectRatio' => false, + ], + ], + 'pageLogo' => [ + 'allowSvgImage' => true, + ], + 'pageLogoMobile' => [ + 'allowSvgImage' => true, + ], + 'coverPhoto' => [ + 'size' => [ + 'minWidth' => UserCoverPhoto::MIN_WIDTH, + 'minHeight' => UserCoverPhoto::MIN_HEIGHT, + ] + ], + 'favicon' => [ + 'size' => [ + 'resize' => false, + 'minWidth' => Style::FAVICON_IMAGE_WIDTH, + 'maxWidth' => Style::FAVICON_IMAGE_WIDTH, + 'minHeight' => Style::FAVICON_IMAGE_HEIGHT, + 'maxHeight' => Style::FAVICON_IMAGE_HEIGHT, + ] + ], ]; } @@ -453,133 +482,63 @@ class StyleAddForm extends AbstractForm { * @since 5.3 */ protected function validateUploads() { - // Preview image. - $field = 'image'; - $files = UploadHandler::getInstance()->getFilesByFieldId($field); - if (count($files) > 1) { - throw new UserInputException($field, 'invalid'); - } - if (!empty($files)) { - $fileLocation = $files[0]->getLocation(); - if (($imageData = getimagesize($fileLocation)) === false) { - throw new UserInputException($field, 'invalid'); - } - switch ($imageData[2]) { - case IMAGETYPE_PNG: - case IMAGETYPE_JPEG: - case IMAGETYPE_GIF: - // fine - break; - default: - throw new UserInputException($field, 'invalid'); - } - - if ($imageData[0] > (Style::PREVIEW_IMAGE_MAX_WIDTH) || $imageData[1] > (Style::PREVIEW_IMAGE_MAX_HEIGHT)) { - $adapter = ImageHandler::getInstance()->getAdapter(); - $adapter->loadFile($fileLocation); - $thumbnail = $adapter->createThumbnail(Style::PREVIEW_IMAGE_MAX_WIDTH, Style::PREVIEW_IMAGE_MAX_HEIGHT, false); - $adapter->writeImage($thumbnail, $fileLocation); - } - } - - // Preview image (2x). - $field = 'image2x'; - $files = UploadHandler::getInstance()->getFilesByFieldId($field); - if (count($files) > 1) { - throw new UserInputException($field, 'invalid'); - } - if (!empty($files)) { - $fileLocation = $files[0]->getLocation(); - if (($imageData = getimagesize($fileLocation)) === false) { - throw new UserInputException($field, 'invalid'); - } - switch ($imageData[2]) { - case IMAGETYPE_PNG: - case IMAGETYPE_JPEG: - case IMAGETYPE_GIF: - // fine - break; - default: - throw new UserInputException($field, 'invalid'); - } - - if ($imageData[0] > (Style::PREVIEW_IMAGE_MAX_WIDTH * 2) || $imageData[1] > (Style::PREVIEW_IMAGE_MAX_HEIGHT * 2)) { - $adapter = ImageHandler::getInstance()->getAdapter(); - $adapter->loadFile($fileLocation); - $thumbnail = $adapter->createThumbnail(Style::PREVIEW_IMAGE_MAX_WIDTH * 2, Style::PREVIEW_IMAGE_MAX_HEIGHT * 2, false); - $adapter->writeImage($thumbnail, $fileLocation); - } - } - - // pageLogo - $field = 'pageLogo'; - $files = UploadHandler::getInstance()->getFilesByFieldId($field); - if (count($files) > 1) { - throw new UserInputException($field, 'invalid'); - } - - // pageLogoMobile - $field = 'pageLogoMobile'; - $files = UploadHandler::getInstance()->getFilesByFieldId($field); - if (count($files) > 1) { - throw new UserInputException($field, 'invalid'); - } - - // coverPhoto - $field = 'coverPhoto'; - $files = UploadHandler::getInstance()->getFilesByFieldId($field); - if (count($files) > 1) { - throw new UserInputException($field, 'invalid'); - } - if (!empty($files)) { - $fileLocation = $files[0]->getLocation(); - if (($imageData = getimagesize($fileLocation)) === false) { + foreach ($this->getUploadFields() as $field => $options) { + $files = UploadHandler::getInstance()->getFilesByFieldId($field); + if (count($files) > 1) { throw new UserInputException($field, 'invalid'); } - switch ($imageData[2]) { - case IMAGETYPE_PNG: - case IMAGETYPE_JPEG: - case IMAGETYPE_GIF: - // fine - break; - default: - throw new UserInputException($field, 'invalid'); - } + if (empty($files)) continue; - if ($imageData[0] < UserCoverPhoto::MIN_WIDTH) { - throw new UserInputException($field, 'minWidth'); - } - if ($imageData[1] < UserCoverPhoto::MIN_HEIGHT) { - throw new UserInputException($field, 'minHeight'); - } - } - - // favicon - $field = 'favicon'; - $files = UploadHandler::getInstance()->getFilesByFieldId($field); - if (count($files) > 1) { - throw new UserInputException($field, 'invalid'); - } - if (!empty($files)) { - $fileLocation = $files[0]->getLocation(); - if (($imageData = getimagesize($fileLocation)) === false) { - throw new UserInputException($field, 'invalid'); - } - switch ($imageData[2]) { - case IMAGETYPE_PNG: - case IMAGETYPE_JPEG: - case IMAGETYPE_GIF: - // fine - break; - default: + if (isset($options['size'])) { + $fileLocation = $files[0]->getLocation(); + if (($imageData = getimagesize($fileLocation)) === false) { throw new UserInputException($field, 'invalid'); - } - - if ($imageData[0] != Style::FAVICON_IMAGE_WIDTH) { - throw new UserInputException($field, 'dimensions'); - } - if ($imageData[1] != Style::FAVICON_IMAGE_HEIGHT) { - throw new UserInputException($field, 'dimensions'); + } + switch ($imageData[2]) { + case IMAGETYPE_PNG: + case IMAGETYPE_JPEG: + case IMAGETYPE_GIF: + // fine + break; + default: + throw new UserInputException($field, 'invalid'); + } + + $maxWidth = $options['size']['maxWidth'] ?? PHP_INT_MAX; + $maxHeight = $options['size']['maxHeight'] ?? PHP_INT_MAX; + $minWidth = $options['size']['maxWidth'] ?? 0; + $minHeight = $options['size']['maxHeight'] ?? 0; + + if ($options['size']['resize'] ?? true) { + if ($imageData[0] > $maxWidth || $imageData[1] > $maxHeight) { + $adapter = ImageHandler::getInstance()->getAdapter(); + $adapter->loadFile($fileLocation); + $thumbnail = $adapter->createThumbnail( + $maxWidth, + $maxHeight, + $options['size']['preserveAspectRatio'] ?? true + ); + $adapter->writeImage($thumbnail, $fileLocation); + } + + // Check again after scaling + if (($imageData = getimagesize($fileLocation)) === false) { + throw new UserInputException($field, 'invalid'); + } + } + + if ($imageData[0] > $maxWidth) { + throw new UserInputException($field, 'maxWidth'); + } + if ($imageData[0] > $maxHeight) { + throw new UserInputException($field, 'maxHeight'); + } + if ($imageData[0] < $minWidth) { + throw new UserInputException($field, 'minWidth'); + } + if ($imageData[0] < $minHeight) { + throw new UserInputException($field, 'minHeight'); + } } } } -- 2.20.1