Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Asset] Stream image thumbnail action improvements #643

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 44 additions & 28 deletions src/Asset/MappedParameter/ImageDownloadConfigParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,21 @@
{
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,
private ?int $quality = 85,
private ?int $dpi = null,
private ?string $positioning = 'center',
private string $cover = 'false',
private string $frame = 'false',
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,21 @@
#[Attribute(Attribute::TARGET_METHOD)]
final class ResizeModeParameter extends QueryParameter
{
public function __construct()
{
public function __construct(
bool $required = true,
array $resizeModes = ResizeModes::ALLOWED_MODES,
string $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
Loading