diff --git a/src/Asset/Controller/Image/CustomStreamController.php b/src/Asset/Controller/Image/CustomStreamController.php index 9411fd2c..64f2ea8a 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 f4b3ab8e..25d04652 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 fe2d1546..70b4d6aa 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 848acb31..c1d65385 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 7eb514fa..4721465d 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 179c8878..8d89001d 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,