Skip to content

Commit

Permalink
Merge pull request #15646 from craftcms/feature/transform-params
Browse files Browse the repository at this point in the history
Allow DI on ImageTransform
  • Loading branch information
brandonkelly authored Jan 11, 2025
2 parents 1e74bbc + 15a4f47 commit 11105f3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 57 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/ImageTransformsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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');
Expand Down
11 changes: 1 addition & 10 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 11 additions & 34 deletions src/helpers/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand Down
6 changes: 4 additions & 2 deletions src/services/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
16 changes: 8 additions & 8 deletions src/services/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]),
);
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 11105f3

Please sign in to comment.