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..a050d4415ed 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->attributes(); $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;