Follow-up to #2420751: ImageEffectBase::transformDimensions() should have a sane default implementation
Problem/Motivation
All dimensions can be calculated, with a sole exception: when you use the rotation image effect with the "random rotation" setting enabled. But I can't imagine that's a very frequently used one. And even then… we could make it "deterministically random" if we'd want to: we could take the hash of the file name and map that to a float/integer between 0 and 360, and voila: deterministically random.
Comment | File | Size | Author |
---|---|---|---|
#28 | 2420789-28.patch | 4.34 KB | mondrake |
#25 | interdiff_22-25.txt | 955 bytes | mondrake |
#22 | 2420789-22-do-not-test.patch | 4.34 KB | mondrake |
Comments
Comment #1
attiks CreditAttribution: attiks commentedsee also #1551686: Image rotate dimensions callback can calculate new dimensions for every angle
Comment #2
Wim LeersHrm, this would require an API change;
ImageEffectInterface::transformDimensions()
would also have to receive the file URL. (Currently it only receives the dimensions of an image, which is insufficient — all images with the same dimensions would get the same rotation applied if we were to use that. That feels like it's "insufficiently random".)But then it would be able to perform file operations. So it'd probably be better to receive a hash of the filename. Something like a
$deterministic_randomness_hash
parameter would probably make most sense.Let's get feedback on that.
Comment #3
Wim LeersComment #4
attiks CreditAttribution: attiks commentedI think it makes a lot of sense to also provide the file name, so image effects doing fancy stuff can use the image to do the calculations, but I guess that is another issue
Comment #5
Wim Leers#4: yeah, it could make sense, but the danger in doing that is that operations that are now guaranteed to not cause I/O, might start causing I/O. That's why I proposed
$deterministic_randomness_hash
in #2.So, yes, definitely another issue.
Comment #6
mondrake#2, note that #1364670: ImageStyle::transformDimensions unable to deal with all effects. is proposing to add the URI to transformDimensions() signature.
A 'deterministic randomness hash' seems to me too specific for this use case. In the end, it would be able to perform I/O, but shall we babysit what you can do with the URI? If you just calculate a hash with it, then no problem!
Comment #7
Wim Leers#6: well, that really is the fundamental question: do we guarantee no I/O happens at the interface level, or don't we? If we don't, we could just as well pass
ImageInterface $image
, which would be consistent withpublic function applyEffect(ImageInterface $image)
on the same interface.Comment #8
mondrake#7 well passing
ImageInterface $image
is 100% I/O guaranteed ;)I looked at where
transformDimension()
is called. We have two instances in HEAD, that basically can be reconducted to processing themes 'image_style' and 'responsive_image'. In both cases we do not have there an ImageInterface object instantiated before callingtransformDimensions()
, just the URI passed in$variables
to the theme. For image_style, the actual loading and application of effects to the image is deferred to the file download controller that only runscreateDerivative()
if the derivative file URI is missing - i.e. only once per source image.So, that would mean to add getting the
$image
object from theimage.factory
in the theme preprocess before callingtransformDimensions()
. Since all new image objects parse their file upon construction via the toolkit, we would have file I/O always, even if not needed.So IMHO if we want to add anything, the URI is still the best option -
Comment #9
mgiffordComment #10
fietserwinI fully agree with #8 and as such propose to postpone this issue on #1364670: ImageStyle::transformDimensions unable to deal with all effects.. The other issue should add the parameter, this issue can then make use of it to get "deterministic randomness" for the random rotation effect.
Comment #11
mondrakeI suppose this would also need to wait for #1551686: Image rotate dimensions callback can calculate new dimensions for every angle, as currently only rotations of multiples of 90 degrees can be managed by
RotateImageEffect::transformDimensions
.Comment #12
mondrake#1364670: ImageStyle::transformDimensions unable to deal with all effects. has been committed.
Comment #13
Wim LeersHurray!
Comment #14
mgiffordComment #15
fietserwinStill postponed on another issue: #1551686: Image rotate dimensions callback can calculate new dimensions for every angle, as we do not yet calculate dimensions for angles that are not a multiple of 90 degrees. However the code changes/new features here will not interfere with code changes/new features in the other issue, it's only that the ultimate goal of this issue depends on both issues being resolved.
So, we could post a patch here that makes the random angle deterministic, but in itself that won't make transformDimensions() work for rotate in general. But because of this, not setting back to postponed.
Comment #16
mondrakeComment #17
mondrakePatch.
This is built on top of #1551686-29: Image rotate dimensions callback can calculate new dimensions for every angle. It's quite simple now :)
The do-not-test patch is the incremental one for review, the other is the full patch for testing.
Comment #18
mondrakeJust realized that in random rotation, the degrees set in configuration define the maximum angle of rotation.
Comment #19
fietserwinI didn't do a thorough review yet but saw that rotation angles are defined as int while they are floats: you can define an angle of 3.5 degrees. Besides some casts and phpdoc comments, this may also influence the % operator (not sure how that operator treats floats) and thus e.g. the check for angles that are supposed to be a multiple of 90 degrees.
Comment #20
mondrake#19 - both the 'degrees' field in the Image effect form, and the config schema, only accept integer values. The GD toolkit ops may take floats (but I wonder if that makes sense anyway...). Supporting floats at effect level would then be a different issue.
Comment #21
fietserwinHmmm, then that would be a regression from D7 where I tested it yesterday and where, as it appears, function image_effect_integer_validate() DOES accept float values and so it works. I have filed a separate issue for this: #2576639: "Regression": accept floats for degrees in rotate effect.
Comment #22
mondrakeRerolled against latest patch in #1551686: Image rotate dimensions callback can calculate new dimensions for every angle.
Also, since
hash()
returns a hex string, changed::getRotationFromUri
to convert that to integer viahexdec()
instead of casting to int.Comment #23
fietserwinThis is only for the changes made here compared to the patch in the related issue #1551686: Image rotate dimensions callback can calculate new dimensions for every angle.
Change to (something like):
An angle in the range from -$max_degrees to +$max_degrees.
the second check falls entirely within the first check. Simplify the whole body to:
$max_degrees = abs((int) $max_degrees);
return $max_degrees === 0 ? 0 : (hexdec(hash(...;
Comment #24
mondrake#1551686: Image rotate dimensions callback can calculate new dimensions for every angle has been committed, so this can be now worked on actively. Leaving to CNW for input in #23.
Comment #25
mondrakeRerolled and done #23.1 and .2
Comment #28
mondrakeReroll
Comment #42
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.
This appears it could still be relevant as the code is still similar.
Tagging for an issue summary update though for steps and proposed solution that matches the fix.
Comment #43
mondrakeThis was implemented in contrib Image Effects module, #3246867: Introduce 'Rotate' effect and operations, alternative to Drupal core's. It's such a very specific use case I wonder if it makes sense to be addressed in core.
Also see #3240390: [policy, no patch] Move image rotation code from core into contrib.
Comment #44
smustgrave CreditAttribution: smustgrave at Mobomo commentedIf you're able to update the IS and reroll of the patch I can review again to help move the policy along.
Comment #45
mondrakeSorry, I'm not interested here. If and when the policy issue is approved, I will be happy to provide deprecation patches.
I would just suggest to won't fix this issue unless there's anyone really interested to move it forward.
Comment #46
smustgrave CreditAttribution: smustgrave at Mobomo commentedGoing to move to PNMI then since it was just sitting there for 7 years.
Comment #47
mondrakeHonestly… let’s see if anyone is interested. In that case, please reopen.