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

Watermark not working #275

Open
1 of 3 tasks
bymayo opened this issue Nov 19, 2024 · 4 comments
Open
1 of 3 tasks

Watermark not working #275

bymayo opened this issue Nov 19, 2024 · 4 comments

Comments

@bymayo
Copy link

bymayo commented Nov 19, 2024

I'm submitting a...

  • bug report
  • feature request
  • general question

Description

I'm trying to use the watermark feature, and it doesn't seem to be rendering anything no matter which way I try to implement this. Ideally i want to do this in the imager-x-transforms.php file like this:

'awardSilver' => [
        'transforms' => [
            ['width' => 400],
            ['width' => 800],
            ['width' => 1200],
            ['width' => 1600],
            ['width' => 1800]
        ],
        'defaults' => [
            'watermark' => [ 
                'image' => '@webroot/dist/images/watermark-award-silver.png', 
                'width' => 80, 
                'height' => 80, 
                'position' => [ 
                    'bottom' => 30, 
                    'right' => 30 
                ], 
                'opacity' => 1
            ]
        ]
    ],

I've also tried using an Asset model by using:

'image' => Craft::$app->getAssets()->getAssetById(353),

And i've also tried this inside the template using Twig, by setting this similar to how it says in the docs.

But still no luck!

I can see the transform is rendering fine, because if I change the width params, it recreates a new file. But does nothing with the watermark.

Displayed errors, stack trace, relevant logs

n/a, I can't see any errors appearing in the logs

Additional info

CleanShot 2024-11-19 at 10 22 56@2x

@aelvan
Copy link
Contributor

aelvan commented Nov 22, 2024

Hi,

I think this is due to the fact that the servd transformer only supports a subset of the functionality that Imager itself supports. I'm not familiar with that transformer, Matt from servd made it, but from what I can tell looking at the source, it's very limited in scope, and there's no watermark functionality.

I would reach out to Matt and ask just to be sure though.

@bymayo
Copy link
Author

bymayo commented Nov 25, 2024

@aelvan Okay, I'll reach out to Matt/Joe and see what they can do!

@bymayo
Copy link
Author

bymayo commented Nov 25, 2024

@aelvan It seems theres 2 ways around this with using Servd:

  1. We just bypass using Servd to transform the image and use it only as storage. But the downside here is that we don't get off server transforms. So it will impact the sites speed/CPU heavily.
  2. We only apply watermarks to images if they are in a specific filesystem. But i'm unsure if Imager X allows to specify different configs, per filesystem, or even do this via the template?

Ideally if you could let me know about point 2, then I'll know where to go from there!

@aelvan
Copy link
Contributor

aelvan commented Nov 27, 2024

Point 2 really depends on how you use the named transform currently. Is it used in an auto generate config, or only directly in templates?

In you templates you can always override both the transform, and your config settings, based on whatever data is available to you there. If all you need is to set a different transform depending on the asset volume, you could just do:

craft.imagerx.transformImage(image, image.volume.handle == 'myVolume' ? 'awardSilverWithWatermark' : 'awardSilverWithoutWatermark')

If you'd want to override the transformer you could do:

craft.imagerx.transformImage(image, awardSilver, {}, { transformer: image, image.volume.handle == 'myVolume' ? 'craft' : 'servd' })

If it's used in an auto generate config it might be harder. Most properties, like watermark, can take a function as value, where the image will be passed in as a variable, like:

        'transforms' => [
            'width' => 300,
            'format' => 'jpg',
            'watermark' => function($image) {
                return $image->volume->handle === 'myVolume' ?
                    [
                        ...
                    ] : [
                        ...
                    ];
            }
        ],

My initial thought was that it could be handled this way, but there isn't really a way of "unsetting" the watermark property currently. Ie, if I return an empty array or null from that function, Imager throws an error, because it expects a watermark definition.

This is maybe something that I could/should account for at some broader level in Imager, being able to "unset" a value in transforms may be useful, escpecially in cases like this. But I'll have to consider if there are any potential side-effects that could be introduced by doing that. It kinda feels like it could. In that case, maybe a more narrow solution is fine.

But, let me know if that's something that's even relevant to what you're trying to achieve.

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

No branches or pull requests

2 participants