diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 69f0956d17d..4d5b69880bd 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -135,6 +135,7 @@ - `craft\helpers\DateTimeHelper::humanDuration()` now has a `$language` argument. ([#16332](https://github.com/craftcms/cms/pull/16332)) - `craft\models\Site` now implements `craft\base\Chippable`. - `craft\services\Revisions::createRevision()` no longer creates the revision if an `EVENT_BEFORE_CREATE_REVISION` event handler sets `$event->handled` to `true` and at least one revision already exists for the element. ([#16260](https://github.com/craftcms/cms/discussions/16260)) +- `craft\models\ImageTransform` objects are now instantiated via `Craft::createObject()`. ([#15646](https://github.com/craftcms/cms/pull/15646)) - Elements’ `defineCardAttributes()` methods can now return a `placeholder` value set to a callable. - Deprecated `craft\elements\User::EVENT_REGISTER_USER_ACTIONS`. - Deprecated `craft\elements\User::IMPERSONATE_KEY`. `craft\web\User::getImpersonatorId()` should be used instead. diff --git a/src/controllers/AssetsController.php b/src/controllers/AssetsController.php index 3466a5b3e34..d72db5c2e27 100644 --- a/src/controllers/AssetsController.php +++ b/src/controllers/AssetsController.php @@ -1318,7 +1318,11 @@ 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 fee47c0eb4b..1439dab389c 100644 --- a/src/controllers/ImageTransformsController.php +++ b/src/controllers/ImageTransformsController.php @@ -90,7 +90,7 @@ public function actionEdit(?string $transformHandle = null, ?ImageTransform $tra throw new NotFoundHttpException('Transform not found'); } } else { - $transform = new ImageTransform(); + $transform = Craft::createObject(ImageTransform::class); } } @@ -144,7 +144,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 e7e2a4a6738..d954b6bf39c 100644 --- a/src/elements/Asset.php +++ b/src/elements/Asset.php @@ -1956,16 +1956,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..8b54a4a31e6 100644 --- a/src/helpers/ImageTransforms.php +++ b/src/helpers/ImageTransforms.php @@ -111,17 +111,7 @@ 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', - ]; + $attributes = $transform->attributes(); $nullables = [ 'id', @@ -131,15 +121,14 @@ public static function extendTransform(ImageTransform $transform, array $paramet 'parameterChangeTime', ]; - foreach ($parameters as $parameter => $value) { - if (in_array($parameter, $whiteList, true)) { - /** @phpstan-ignore-next-line */ - $transform->$parameter = $value; + foreach ($parameters as $name => $value) { + if (in_array($name, $attributes, true)) { + $transform->$name = $value; } } - foreach ($nullables as $nullable) { - $transform->{$nullable} = null; + foreach ($nullables as $name) { + $transform->$name = null; } } @@ -289,22 +278,7 @@ public static function normalizeTransform(mixed $transform): ?ImageTransform } if (is_object($transform)) { - $transform = ArrayHelper::toArray($transform, [ - 'id', - 'name', - 'transformer', - 'handle', - 'width', - 'height', - 'format', - 'parameterChangeTime', - 'mode', - 'position', - 'fill', - 'upscale', - 'quality', - 'interlace', - ]); + $transform = ArrayHelper::toArray($transform); } if (is_array($transform)) { @@ -333,7 +307,10 @@ public static function normalizeTransform(mixed $transform): ?ImageTransform return self::extendTransform($baseTransform, $transform); } - return new ImageTransform($transform); + return Craft::createObject([ + 'class' => ImageTransform::class, + ...$transform, + ]); } if (is_string($transform)) { diff --git a/src/services/Assets.php b/src/services/Assets.php index 1c9e90e4ea8..bc89b5d6ec2 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 c3ee7aab578..9d2c54d2cc6 100644 --- a/src/services/ImageTransforms.php +++ b/src/services/ImageTransforms.php @@ -128,7 +128,10 @@ 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([ + 'class' => ImageTransform::class, + ...$result, + ]), ); } @@ -420,13 +423,10 @@ 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;