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

Draft
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
]) : [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting all attributes instead of specifying, so we pick up any defined by a different class.

Alternatively, we could introduce something like \craft\models\ImageTransform::getImageAttributes that returned the attribute names involved in the actual image manipulation.

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

$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