Skip to content

Allow DI on ImageTransform#15646

Merged
brandonkelly merged 10 commits into5.6from
feature/transform-params
Jan 11, 2025
Merged

Allow DI on ImageTransform#15646
brandonkelly merged 10 commits into5.6from
feature/transform-params

Conversation

@timkelty
Copy link
Copy Markdown
Contributor

@timkelty timkelty commented Sep 3, 2024

Description

The goal is to allow developers to write an ImageTransformer that builds URLs using properties other than those included in \craft\models\ImageTransform, eg blur.

This PR does 2 things:

  • Replaces new instance creation with createObject so \craft\models\ImageTransform can be customized through DI.
  • Calls \craft\models\ImageTransform::toArray without any args, so that all properties are returned.

Related issues

#15062

Example usage

Module/Plugin

<?php

namespace modules\testmodule;

use Craft;
use craft\elements\Asset;
use craft\events\DefineAssetUrlEvent;
use craft\helpers\UrlHelper;
use craft\models\ImageTransform;
use yii\base\Event;
use yii\base\Module as BaseModule;

/**
 * @method static Module getInstance()
 */
class Module extends BaseModule
{
    public function init(): void
    {
        parent::init();

        // Defer most setup tasks until Craft is fully initialized
        Craft::$app->onInit(function() {
            Craft::$container->set(ImageTransform::class, [
                'class' => \modules\testmodule\ImageTransform::class,
            ]);


            Event::on(
                Asset::class,
                Asset::EVENT_DEFINE_URL,
                function (DefineAssetUrlEvent $event) {
                    $transform = $event->transform;

                    if (!$transform) {
                        return;
                    }

                    /** @var Asset $asset */
                    $asset = $event->sender;

                    $url = UrlHelper::urlWithParams(
                        "https://foo.com/$asset->path",
                        $transform,
                    );

                    $event->url = $url;
                });
        });
    }
}
<?php

namespace modules\testmodule;

class ImageTransform extends \craft\models\ImageTransform
{
    public int $blur = 0;
}

Template

{% set transform = {
    mode: 'crop',
    width: 100,
    height: 100,
    quality: 75,
    blur: 40,
} %}
{% set asset = craft.assets().one() %}

{{ asset.getUrl(transform) }}
{{ asset.getImg(transform, ['1.5x', '2.0']) }}

Comment thread src/elements/Asset.php
@leevigraham
Copy link
Copy Markdown
Contributor

@timkelty Finally got round to testing this and it's perfecto!

I forked the 5.x branch and rebased your changes on top. Everything worked fine.

This looks like a simple enough change it could be merged into Craft 4 as well?

image

@leevigraham
Copy link
Copy Markdown
Contributor

@timkelty What's needed to get this merged :)

@leevigraham
Copy link
Copy Markdown
Contributor

@timkelty I had another play with this PR and I can reduce it to just removing the filtering on the ->toArray() methods and the $whitelist.

I can pass through the behaviours custom property by using the EVENT_DEFINE_FIELDS event.

Event::on(
    ImageTransform::class,
    Model::EVENT_DEFINE_FIELDS,
    function (DefineFieldsEvent $event) {
        $event->fields['imgixParams'] = 'imgixParams';
    }
);

@timkelty timkelty marked this pull request as ready for review January 10, 2025 17:32
@brandonkelly brandonkelly changed the base branch from 5.x to 5.6 January 11, 2025 02:13
@brandonkelly brandonkelly merged commit 11105f3 into 5.6 Jan 11, 2025
@brandonkelly brandonkelly deleted the feature/transform-params branch January 11, 2025 02:38
@leevigraham
Copy link
Copy Markdown
Contributor

Yewww!

@brandonkelly
Copy link
Copy Markdown
Member

Craft 5.6.0 is out now 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants