From 5d0eeba70ee07b201193b200d2fc53ff2e2bcd8e Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Tue, 3 Sep 2024 13:40:52 -0400 Subject: [PATCH 1/2] Allow DI on ImageTransform --- src/controllers/AssetsController.php | 5 ++++- src/controllers/ImageTransformsController.php | 4 ++-- src/elements/Asset.php | 11 +---------- src/helpers/ImageTransforms.php | 17 ++++------------- src/services/Assets.php | 6 ++++-- src/services/ImageTransforms.php | 12 ++++-------- 6 files changed, 19 insertions(+), 36 deletions(-) diff --git a/src/controllers/AssetsController.php b/src/controllers/AssetsController.php index 3ecccb3fd93..66eb6119ed3 100644 --- a/src/controllers/AssetsController.php +++ b/src/controllers/AssetsController.php @@ -1318,7 +1318,10 @@ public function actionGenerateFallbackTransform(string $transform): Response if ($useOriginal) { $ext = $asset->getExtension(); } else { - $transform = new ImageTransform(ImageTransforms::parseTransformString($transformString)); + $transform = Craft::createObject([ + 'class' => ImageTransform::class, + ] + ImageTransforms::parseTransformString($transformString)); + $ext = $transform->format ?: ImageTransforms::detectTransformFormat($asset); } diff --git a/src/controllers/ImageTransformsController.php b/src/controllers/ImageTransformsController.php index 45e581a61ae..168d30a6b9c 100644 --- a/src/controllers/ImageTransformsController.php +++ b/src/controllers/ImageTransformsController.php @@ -74,7 +74,7 @@ public function actionEdit(?string $transformHandle = null, ?ImageTransform $tra throw new NotFoundHttpException('Transform not found'); } } else { - $transform = new ImageTransform(); + $transform = Craft::createObject(ImageTransform::class); } } @@ -127,7 +127,7 @@ public function actionSave(): ?Response { $this->requirePostRequest(); - $transform = new ImageTransform(); + $transform = Craft::createObject(ImageTransform::class); $transform->id = $this->request->getBodyParam('transformId'); $transform->name = $this->request->getBodyParam('name'); $transform->handle = $this->request->getBodyParam('handle'); diff --git a/src/elements/Asset.php b/src/elements/Asset.php index 23a7df85ba9..f8fe0302ab5 100644 --- a/src/elements/Asset.php +++ b/src/elements/Asset.php @@ -1888,16 +1888,7 @@ public function getUrlsBySize(array $sizes, mixed $transform = null): array [$value, $unit] = Assets::parseSrcsetSize($size); - $sizeTransform = $transform ? $transform->toArray([ - 'format', - 'height', - 'interlace', - 'mode', - 'position', - 'quality', - 'width', - 'fill', - ]) : []; + $sizeTransform = $transform ? $transform->toArray() : []; if ($unit === 'w') { $sizeTransform['width'] = (int)$value; diff --git a/src/helpers/ImageTransforms.php b/src/helpers/ImageTransforms.php index ef7a5222969..ff53d74689a 100644 --- a/src/helpers/ImageTransforms.php +++ b/src/helpers/ImageTransforms.php @@ -111,17 +111,8 @@ public static function extendTransform(ImageTransform $transform, array $paramet // Don't change the same transform $transform = clone $transform; - $whiteList = [ - 'width', - 'height', - 'format', - 'mode', - 'format', - 'position', - 'quality', - 'interlace', - 'transformer', - ]; + // Why aren't we just using setAttributes()? + $whiteList = $transform->getAttributes(); $nullables = [ 'id', @@ -133,7 +124,6 @@ public static function extendTransform(ImageTransform $transform, array $paramet foreach ($parameters as $parameter => $value) { if (in_array($parameter, $whiteList, true)) { - /** @phpstan-ignore-next-line */ $transform->$parameter = $value; } } @@ -288,6 +278,7 @@ public static function normalizeTransform(mixed $transform): ?ImageTransform return $transform; } + // TODO: review if this is dead code. With $transform typed as mixed, it is unclear what kind of object this would be. stdObject? if (is_object($transform)) { $transform = ArrayHelper::toArray($transform, [ 'id', @@ -333,7 +324,7 @@ public static function normalizeTransform(mixed $transform): ?ImageTransform return self::extendTransform($baseTransform, $transform); } - return new ImageTransform($transform); + return Craft::createObject(ImageTransform::class, [$transform]); } if (is_string($transform)) { diff --git a/src/services/Assets.php b/src/services/Assets.php index 033b43092a1..19c70d6044d 100644 --- a/src/services/Assets.php +++ b/src/services/Assets.php @@ -680,7 +680,8 @@ public function getThumbUrl(Asset $asset, int $width, ?int $height = null, $icon return $iconFallback ? AssetsHelper::iconUrl($extension) : null; } - $transform = new ImageTransform([ + $transform = Craft::createObject([ + 'class' => ImageTransform::class, 'width' => $width, 'height' => $height, 'mode' => 'crop', @@ -724,7 +725,8 @@ public function getImagePreviewUrl(Asset $asset, int $maxWidth, int $maxHeight): $originalWidth > $width || $originalHeight > $height ) { - $transform = new ImageTransform([ + $transform = Craft::createObject([ + 'class' => ImageTransform::class, 'width' => $width, 'height' => $height, 'mode' => 'crop', diff --git a/src/services/ImageTransforms.php b/src/services/ImageTransforms.php index ad719ee6d0a..4e7646560be 100644 --- a/src/services/ImageTransforms.php +++ b/src/services/ImageTransforms.php @@ -128,7 +128,7 @@ private function _transforms(): MemoizableArray if (!isset($this->_transforms)) { $this->_transforms = new MemoizableArray( $this->_createTransformQuery()->all(), - fn(array $result) => new ImageTransform($result), + fn(array $result) => Craft::createObject(ImageTransform::class, [$result]), ); } @@ -420,13 +420,9 @@ public function eagerLoadTransforms(array $assets, array $transforms): void throw new InvalidArgumentException("Can’t eager-load transform “{$transform}” without a prior transform that specifies the base width"); } - $transform = new ImageTransform($refTransform->toArray([ - 'format', - 'interlace', - 'mode', - 'position', - 'quality', - ])); + $transform = Craft::createObject([ + 'class' => ImageTransform::class, + ] + $refTransform->toArray()); if ($sizeUnit === 'w') { $transform->width = (int)$sizeValue; From ab202651cb4fb226d2e4cb78946684e4dff60fc3 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Tue, 3 Sep 2024 14:20:20 -0400 Subject: [PATCH 2/2] attribute names --- src/helpers/ImageTransforms.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/ImageTransforms.php b/src/helpers/ImageTransforms.php index ff53d74689a..a050d4415ed 100644 --- a/src/helpers/ImageTransforms.php +++ b/src/helpers/ImageTransforms.php @@ -112,7 +112,7 @@ public static function extendTransform(ImageTransform $transform, array $paramet $transform = clone $transform; // Why aren't we just using setAttributes()? - $whiteList = $transform->getAttributes(); + $whiteList = $transform->attributes(); $nullables = [ 'id',