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

Allow DI on ImageTransform #15646

Merged
merged 10 commits into from
Jan 11, 2025
Merged
5 changes: 4 additions & 1 deletion src/controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

Expand Down Expand Up @@ -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');
Expand Down
11 changes: 1 addition & 10 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]) : [];
brandonkelly marked this conversation as resolved.
Show resolved Hide resolved
$sizeTransform = $transform ? $transform->toArray() : [];

if ($unit === 'w') {
$sizeTransform['width'] = (int)$value;
Expand Down
17 changes: 4 additions & 13 deletions src/helpers/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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)) {
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
12 changes: 4 additions & 8 deletions src/services/ImageTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
);
}

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