From 81288be81d477029397b8c602dae61e3283515d7 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Mon, 16 Dec 2024 15:52:43 +0100 Subject: [PATCH 1/4] ResizeMode optional --- .../Image/CustomStreamController.php | 15 +++- .../ImageDownloadConfigParameter.php | 70 ++++++++++++------- .../Parameter/Query/MimeTypeParameter.php | 4 +- .../Parameter/Query/ResizeModeParameter.php | 17 ++--- src/Asset/Service/ThumbnailService.php | 3 +- src/Util/Constant/Asset/ResizeModes.php | 2 + 6 files changed, 71 insertions(+), 40 deletions(-) diff --git a/src/Asset/Controller/Image/CustomStreamController.php b/src/Asset/Controller/Image/CustomStreamController.php index 9411fd2c3..64f2ea8a1 100644 --- a/src/Asset/Controller/Image/CustomStreamController.php +++ b/src/Asset/Controller/Image/CustomStreamController.php @@ -40,6 +40,8 @@ use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse; use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags; use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface; +use Pimcore\Bundle\StudioBackendBundle\Util\Constant\Asset\MimeTypes; +use Pimcore\Bundle\StudioBackendBundle\Util\Constant\Asset\ResizeModes; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseHeaders; use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions; @@ -84,8 +86,17 @@ public function __construct( tags: [Tags::Assets->name] )] #[IdParameter(type: 'image')] - #[MimeTypeParameter] - #[ResizeModeParameter] + #[MimeTypeParameter(MimeTypes::PNG->value)] + #[ResizeModeParameter( + true, + [ + ResizeModes::SCALE_BY_HEIGHT, + ResizeModes::SCALE_BY_WIDTH, + ResizeModes::RESIZE, + ResizeModes::NONE + ], + ResizeModes::NONE + )] #[ImageConfigParameter('width', 140)] #[ImageConfigParameter('height')] #[ImageConfigParameter('quality', 85)] diff --git a/src/Asset/MappedParameter/ImageDownloadConfigParameter.php b/src/Asset/MappedParameter/ImageDownloadConfigParameter.php index f4b3ab8e1..25d046526 100644 --- a/src/Asset/MappedParameter/ImageDownloadConfigParameter.php +++ b/src/Asset/MappedParameter/ImageDownloadConfigParameter.php @@ -28,7 +28,7 @@ { public function __construct( private string $mimeType, - private string $resizeMode, + private string $resizeMode = ResizeModes::NONE, private ?int $width = null, private ?int $height = null, private ?int $quality = null, @@ -39,34 +39,10 @@ public function __construct( private string $contain = 'false', private string $forceResize = 'false', ) { - if (!in_array($this->mimeType, [MimeTypes::JPEG->value, MimeTypes::PNG->value], true)) { - throw new InvalidArgumentException('Invalid mime type' . $this->mimeType); - } - if (!in_array($this->resizeMode, ResizeModes::ALLOWED_MODES)) { - throw new InvalidArgumentException('Invalid resize mode ' . $this->resizeMode); - } - - if ($this->resizeMode === ResizeModes::SCALE_BY_HEIGHT && !$this->isValidHeight()) { - throw new InvalidArgumentException( - 'Height must be set and non-negative when using scale by width resize mode' - ); - } + $this->validateResizeMode(); - if ($this->resizeMode === ResizeModes::SCALE_BY_WIDTH && !$this->isValidWidth()) { - throw new InvalidArgumentException( - 'Width must be set and non-negative when using scale by width resize mode' - ); - } - - if ( - (!$this->isValidWidth() || !$this->isValidHeight()) && - ($this->hasFrame() || $this->hasCover() || $this->hasContain() || $this->resizeMode === ResizeModes::RESIZE) - ) { - throw new InvalidArgumentException( - 'Width, height must be set and non-negative when using frame, cover, contain or resize' - ); - } + $this->validateTransformations(); } public function getMimeType(): string @@ -163,4 +139,44 @@ private function getBaseTransformationValues(): array 'forceResize' => $this->getForceResize(), ]; } + + private function validateResizeMode(): void + { + if($this->resizeMode === ResizeModes::NONE) { + return; + } + + if (!in_array($this->mimeType, [MimeTypes::JPEG->value, MimeTypes::PNG->value], true)) { + throw new InvalidArgumentException('Invalid mime type' . $this->mimeType); + } + + if ($this->resizeMode === ResizeModes::SCALE_BY_HEIGHT && !$this->isValidHeight()) { + throw new InvalidArgumentException( + 'Height must be set and non-negative when using scale by width resize mode' + ); + } + + if ($this->resizeMode === ResizeModes::SCALE_BY_WIDTH && !$this->isValidWidth()) { + throw new InvalidArgumentException( + 'Width must be set and non-negative when using scale by width resize mode' + ); + } + + if($this->resizeMode === ResizeModes::RESIZE && (!$this->isValidWidth() || !$this->isValidHeight())) { + throw new InvalidArgumentException( + 'Width and height must be set and non-negative when using resize' + ); + } + } + + private function validateTransformations(): void + { + if ((!$this->isValidWidth() || !$this->isValidHeight()) && + ($this->hasFrame() || $this->hasCover() || $this->hasContain()) + ) { + throw new InvalidArgumentException( + 'Width, height must be set and non-negative when using frame, cover, contain or resize' + ); + } + } } diff --git a/src/Asset/OpenApi/Attribute/Parameter/Query/MimeTypeParameter.php b/src/Asset/OpenApi/Attribute/Parameter/Query/MimeTypeParameter.php index fe2d1546b..70b4d6aa6 100644 --- a/src/Asset/OpenApi/Attribute/Parameter/Query/MimeTypeParameter.php +++ b/src/Asset/OpenApi/Attribute/Parameter/Query/MimeTypeParameter.php @@ -24,7 +24,7 @@ #[Attribute(Attribute::TARGET_METHOD)] final class MimeTypeParameter extends QueryParameter { - public function __construct() + public function __construct(string $defaultValue = MimeTypes::JPEG->value) { parent::__construct( name: 'mimeType', @@ -37,7 +37,7 @@ enum: [ MimeTypes::JPEG->value, MimeTypes::PNG->value, ], - example: MimeTypes::JPEG->value + example: $defaultValue ), ); } diff --git a/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php b/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php index 848acb317..c1d653850 100644 --- a/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php +++ b/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php @@ -24,21 +24,22 @@ #[Attribute(Attribute::TARGET_METHOD)] final class ResizeModeParameter extends QueryParameter { - public function __construct() + public function __construct( + bool $required = true, + array $resizeModes = ResizeModes::ALLOWED_MODES, + mixed $defaultValue = ResizeModes::SCALE_BY_WIDTH + ) { parent::__construct( name: 'resizeMode', description: 'Resize mode of downloaded image.', in: 'query', - required: true, + required: $required, + schema: new Schema( type: 'string', - enum: [ - ResizeModes::RESIZE, - ResizeModes::SCALE_BY_WIDTH, - ResizeModes::SCALE_BY_HEIGHT, - ], - example: ResizeModes::SCALE_BY_WIDTH + enum: $resizeModes, + example: $defaultValue ), ); } diff --git a/src/Asset/Service/ThumbnailService.php b/src/Asset/Service/ThumbnailService.php index 7eb514fa6..4721465d0 100644 --- a/src/Asset/Service/ThumbnailService.php +++ b/src/Asset/Service/ThumbnailService.php @@ -229,13 +229,14 @@ private function setThumbnailConfigResizeParameters( 'height' => $resizeHeight, ] ), - default => $thumbnailConfig->addItem( + ResizeModes::RESIZE => $thumbnailConfig->addItem( ResizeModes::RESIZE, [ 'width' => $resizeWidth, 'height' => $resizeHeight, ] ), + default => null }; return $thumbnailConfig; diff --git a/src/Util/Constant/Asset/ResizeModes.php b/src/Util/Constant/Asset/ResizeModes.php index 179c88787..8d89001da 100644 --- a/src/Util/Constant/Asset/ResizeModes.php +++ b/src/Util/Constant/Asset/ResizeModes.php @@ -27,6 +27,8 @@ public const SCALE_BY_HEIGHT = 'scaleByHeight'; + public const NONE = 'none'; + public const ALLOWED_MODES = [ self::RESIZE, self::SCALE_BY_WIDTH, From 13458d8e945f04dbcc0fcf89c4e1331c83cf956e Mon Sep 17 00:00:00 2001 From: mcop1 Date: Mon, 16 Dec 2024 14:56:18 +0000 Subject: [PATCH 2/4] Apply php-cs-fixer changes --- src/Asset/Controller/Image/CustomStreamController.php | 2 +- src/Asset/MappedParameter/ImageDownloadConfigParameter.php | 4 ++-- .../OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Asset/Controller/Image/CustomStreamController.php b/src/Asset/Controller/Image/CustomStreamController.php index 64f2ea8a1..0c66ebb6d 100644 --- a/src/Asset/Controller/Image/CustomStreamController.php +++ b/src/Asset/Controller/Image/CustomStreamController.php @@ -93,7 +93,7 @@ public function __construct( ResizeModes::SCALE_BY_HEIGHT, ResizeModes::SCALE_BY_WIDTH, ResizeModes::RESIZE, - ResizeModes::NONE + ResizeModes::NONE, ], ResizeModes::NONE )] diff --git a/src/Asset/MappedParameter/ImageDownloadConfigParameter.php b/src/Asset/MappedParameter/ImageDownloadConfigParameter.php index 25d046526..f9386cac1 100644 --- a/src/Asset/MappedParameter/ImageDownloadConfigParameter.php +++ b/src/Asset/MappedParameter/ImageDownloadConfigParameter.php @@ -142,7 +142,7 @@ private function getBaseTransformationValues(): array private function validateResizeMode(): void { - if($this->resizeMode === ResizeModes::NONE) { + if ($this->resizeMode === ResizeModes::NONE) { return; } @@ -162,7 +162,7 @@ private function validateResizeMode(): void ); } - if($this->resizeMode === ResizeModes::RESIZE && (!$this->isValidWidth() || !$this->isValidHeight())) { + if ($this->resizeMode === ResizeModes::RESIZE && (!$this->isValidWidth() || !$this->isValidHeight())) { throw new InvalidArgumentException( 'Width and height must be set and non-negative when using resize' ); diff --git a/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php b/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php index c1d653850..7227dbc22 100644 --- a/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php +++ b/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php @@ -28,8 +28,7 @@ public function __construct( bool $required = true, array $resizeModes = ResizeModes::ALLOWED_MODES, mixed $defaultValue = ResizeModes::SCALE_BY_WIDTH - ) - { + ) { parent::__construct( name: 'resizeMode', description: 'Resize mode of downloaded image.', From 2a804c0d308b6e97e041f2dc62e567d3a2e8e6f3 Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Mon, 16 Dec 2024 16:03:05 +0100 Subject: [PATCH 3/4] Added quality default value --- src/Asset/MappedParameter/ImageDownloadConfigParameter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Asset/MappedParameter/ImageDownloadConfigParameter.php b/src/Asset/MappedParameter/ImageDownloadConfigParameter.php index 25d046526..f5874cf6c 100644 --- a/src/Asset/MappedParameter/ImageDownloadConfigParameter.php +++ b/src/Asset/MappedParameter/ImageDownloadConfigParameter.php @@ -31,7 +31,7 @@ public function __construct( private string $resizeMode = ResizeModes::NONE, private ?int $width = null, private ?int $height = null, - private ?int $quality = null, + private ?int $quality = 85, private ?int $dpi = null, private ?string $positioning = 'center', private string $cover = 'false', From 67d7e18dd241dd61e01ef016f9ca91ef0980e99f Mon Sep 17 00:00:00 2001 From: Marco Perberschlager Date: Mon, 16 Dec 2024 16:11:43 +0100 Subject: [PATCH 4/4] Changed type to string --- .../OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php b/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php index 7227dbc22..39f0ddd65 100644 --- a/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php +++ b/src/Asset/OpenApi/Attribute/Parameter/Query/ResizeModeParameter.php @@ -27,7 +27,7 @@ final class ResizeModeParameter extends QueryParameter public function __construct( bool $required = true, array $resizeModes = ResizeModes::ALLOWED_MODES, - mixed $defaultValue = ResizeModes::SCALE_BY_WIDTH + string $defaultValue = ResizeModes::SCALE_BY_WIDTH ) { parent::__construct( name: 'resizeMode',