Updated: Comment #0
Problem/Motivation
ImageStyle::createDerivative is a 'black box', given an image style andi source/target URIs you get effects applied to source to obtain target, but you can not interact with the process.
In a contrib module, I am using an effect to set a canvas, and further effects add on top of that canvas. The 'first' effect is setting a parameter (i.e. the color to use if the canvas has to be resized by following effects) that is 'carried on' along the subsequent effects.
I am saving the setting of the parameter to be carried on in a static variable, and the other effects are reading that variable.
This works fine, but the only potential issue is that to be 'clean', I would like to reset that static variable before I invoke createDerivative, so that there are no risks to use a value that is coming from a previous image style.
Proposed resolution
Introduce a new hook like e.g. hook_image_style_create_derivative that modules can use to do their stuff? Something like hook_image_style_flush.
Remaining tasks
- Agree
- Write patch
User interface changes
None
API changes
One more hook
Related Issues
Comment | File | Size | Author |
---|---|---|---|
#9 | 2098247-style_parameters-9.patch | 16.75 KB | mondrake |
#9 | interdiff_7-9.txt | 773 bytes | mondrake |
#7 | 2098247-style_parameters-7.patch | 16.74 KB | mondrake |
#7 | interdiff_5-7.txt | 3.04 KB | mondrake |
#5 | 2098247-style_parameters-5.patch | 13.71 KB | mondrake |
Comments
Comment #1
mondrakePatch.
Comment #2
claudiu.cristeaHey @mondrake, isn't this related #1826362: ImageEffects of the same image style should be able to pass variables between them?
Check also my comment from #1826362-7: ImageEffects of the same image style should be able to pass variables between them. I wonder how can we merge these 2 issues. Definitely I see the need that effects of the same image style should be able to pass variables between them. I just want to find a more modern way to do this without placing hook invocations all over the code.
Comment #3
dawehnerIt would be good to typehint the image style in the actual function signature.
Comment #4
mondrake#2 - @claudiu.cristea well I guess we may add to the ImageStyle entity a protected variable (being an array of parameters that the effects can get/set) and public getters/setters for the effects to use.
Then passing the $style to applyEffect would enable doing what you say in #2, i.e. passing variables between effects, as you commented in #1826362-7: ImageEffects of the same image style should be able to pass variables between them.
This would be good for my use case (Textimage module sets a background color in an effect, and following effects take that if the image needs to be resized).
However, I believe a hook in this case is still needed if we want to enable modules to initialize those parameters upon calling createDerivative and before the first effect is applied, or to take other actions (e.g. logging the request like in the example in the patch).
I believe I can write a patch in this sense, but opinions first?
Comment #5
mondrakeThis patch combines #1 with #1826362: ImageEffects of the same image style should be able to pass variables between them, plus introduces a 'parameters' protected variable in ImageStyle with getters/setter. The idea is that effects can access a pool of style-level parameters at run-time, during the creation of a derivative. So image effects can effectively transfer information between them if needed.
Comment #7
mondrakeWith tests.
Comment #9
mondrakeOh yes
Comment #10
mondrakeComment #11
fietserwinWhy add a parameter at all?
By passing info via a parameter construct, both effects (the "setting" and "receiving" one) become dependent from each other, while only the setting one should have to know about this. With getConfiguration() and setConfiguration() on ImageEffectInterface (being a ConfigurablePluginInterface), we can already hack into other effects if we have access to the effect.
Thus if a "special canvas" effect wants to influence the (background) color of a subsequent text, resize or rotate effect, that effect should be happily unaware of its context (no way that the core rotate effect is going to use a parameter set by a contrib effect). So, it should be the special canvas effect that alters the color value in the configuration of the rotate, text or whatever effect.
Aside: if you insist on adding a parameter property, make it a key-value store and you don't have to add any adding, setting or deleting method yourself, not in the interface, not in this class. Compare my patch in #2168511: Allow to pass save options to ImageInterface::save.
What context should be passed to an effect?
So are we passing in specific context or too few context? My idea for now: we can add the imageStyle parameter to applyEffect, but make it optional by adding "= NULL".
Comment #12
mondrakeMarking as duplicate of #1826362: ImageEffects of the same image style should be able to pass variables between them, let's continue there. Introduction of ThirdPartySettingsTrait to the ImageStyle solves part of this, we need to make sure that these are accessible from the effects.
Comment #13
mondrake