Skip to content

Commit

Permalink
ResizeMode optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mcop1 committed Dec 16, 2024
1 parent 7cbcc42 commit 81288be
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 40 deletions.
15 changes: 13 additions & 2 deletions src/Asset/Controller/Image/CustomStreamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down
70 changes: 43 additions & 27 deletions src/Asset/MappedParameter/ImageDownloadConfigParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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'
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -37,7 +37,7 @@ enum: [
MimeTypes::JPEG->value,
MimeTypes::PNG->value,
],
example: MimeTypes::JPEG->value
example: $defaultValue
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Asset/Service/ThumbnailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Util/Constant/Asset/ResizeModes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

public const SCALE_BY_HEIGHT = 'scaleByHeight';

public const NONE = 'none';

public const ALLOWED_MODES = [
self::RESIZE,
self::SCALE_BY_WIDTH,
Expand Down

0 comments on commit 81288be

Please sign in to comment.