Problem/Motivation
It would be useful if the Scale and Crop image effect had an anchor option, similar to the basic Crop image effect. The anchor type, such as "center-center", is used to set the top left coordinates of the crop area. Currently, Scale and Crop crops only to the center of an image.
Proposed resolution
Have the ScaleAndCropImageEffect
descend from CropImageEffect
which provides the anchor option, (rather than ScaleImageEffect
which it descends from currently) and do the extra math.
Remaining tasks
Write patch with tests- Review
- Commit!
User interface changes
The "Scale and crop" image effect now gives the option of choosing an anchor - it works exactly like the "Crop" image effect (which the plugin is descending from).
API changes
This adds a backwards compatible change to the 'scale_and_crop' image toolkit operation: it now can optionally accept 'x' and 'y' arguments to give an offset.
This really only affects people who are implementing image toolkits (like the ImageMagick module), who will need to do something with these arguments if provided.
Data model changes
This adds the "anchor" key to the plugin configuration for this image effect. The default value is "center-center" which matches the old behavior, so if someone imports an images style which with a "Scale and crop" effect that doesn't have the "anchor" key, it'll automatically get set to "center-center".
In short, old configuration will continue to import just fine with no change in behavior.
But tests that look at the configuration will see the new key, and on export the new key will be added. This is the cause of the majority of the test changes, and the changes to the config files in the demo_umami profile.
Comment | File | Size | Author |
---|---|---|---|
#101 | 1252606-101.patch | 23.2 KB | rlhawk |
#97 | interdiff.txt | 918 bytes | rlhawk |
#97 | 1252606-97.patch | 23.27 KB | rlhawk |
#91 | interdiff.txt | 898 bytes | rlhawk |
#91 | 1252606-91.patch | 22.73 KB | rlhawk |
Comments
Comment #1
rlhawkHere's a patch that implements that feature.
Comment #3
rlhawkPatch recreated for 8.x branch.
Comment #5
attiks CreditAttribution: attiks commentedFix for failing test
Comment #6
attiks CreditAttribution: attiks commentedComment #7
rlhawkThanks, attiks!
Comment #8
rlhawkComment #9
attiks CreditAttribution: attiks commentedCan you mark it as RTBC if it' working for you
Comment #10
rlhawkI applied the patch and everything works great. Is it unlikely that this would be committed if it were backported to D7?
Comment #11
attiks CreditAttribution: attiks commentedMarking at as such, I assume it isn't a problem, but not my call
Comment #12
webchickFixing tag.
Comment #13
sunBogus indentation change.
While being there, also add a blank phpDoc line before @return, please.
Exceeds 80 chars, missing full-stop/period.
20 days to next Drupal core point release.
Comment #14
attiks CreditAttribution: attiks commentednew patch attached
Comment #15
rlhawkTested with no issues.
Comment #16
sunI'd like someone deeply familiar with the image toolkit API and Image module to RTBC this patch.
Comment #17
andypostIs there explanation about available values? Supose docs should list them
Same. Could use @see to point to a definition list
14 days to next Drupal core point release.
Comment #18
rlhawkNew patch that includes a better description of available values for anchor type. Also added a default value for $anchor in image_scale_and_crop() to avoid breaking any contributed or custom modules that use the function.
Comment #19
rlhawkRerolled patch for new core directory structure.
Comment #20
arbel CreditAttribution: arbel commentedworks as is for d7
Comment #21
Adysone CreditAttribution: Adysone commentedPatch for D7, works on 7.12.
Comment #22
jacquesboucar CreditAttribution: jacquesboucar commentedthe patch is running. i tested in drupal 7.x cloned from github
Comment #23
marcingy CreditAttribution: marcingy commentedPatches need to go into d8 first.
Comment #24
C. Lee CreditAttribution: C. Lee commentedI'm writing this comment since I'd like to see this patch applied soon. The patch #21 is working fine for me.
Comment #25
attiks CreditAttribution: attiks commented#21: scale-and-crop.patch queued for re-testing.
Comment #27
attiks CreditAttribution: attiks commentedPatch needs re roll for /coreclicked the wrong link before.Comment #28
attiks CreditAttribution: attiks commented#19: scale-and-crop-1252606-19.patch queued for re-testing.
Comment #29
Alan Evans CreditAttribution: Alan Evans commentedThere seems to be a problem with editing styles, which I noticed when doing functional testing on this patch. I'm setting this issue back to needs work until i can ascertain whether this issue was introduced by this patch, or if it was also present before patching.
It seems that editing an effect which has a position causes a effect to be added rather than editing the existing. If you save it without changing the position, then it does not create a duplicate effect.
https://skitch.com/e-alanevans/8qeq2/edit-test-post-patch-style-testd85....
Steps to reproduce:
Comment #30
Alan Evans CreditAttribution: Alan Evans commentedOK, I don't think this patch introduced this issue (issue exists also on d8 when not patched), so setting back to needs review and we'll deal with the multiplying image effects elsewhere. The problem is that the effects are referred to by a name derived from their function, so editing an existing effect produces a new unique name, thereby adding it.
Comment #31
Alan Evans CreditAttribution: Alan Evans commentedThe multiplying image effects issue does not exist in D7 as effects are referred-to by an int ID, so that's a new issue in D8, but not introduced here. Created #1508872: Image effects duplicate on edit to track this.
I do have to wonder with this patch whether we should maybe display a description of the orientation in the effect summary. For example, here we have no way of knowing which effect has which orientation (well, you could check the url on hover over "edit", but that's not really usable). https://skitch.com/e-alanevans/8qe9k/edit-test-post-patch-style-testd85....
I'll aim to complete testing and review of this, as that's a minor point.
Comment #32
Alan Evans CreditAttribution: Alan Evans commentedSo, this works as designed in test:
Code review to follow.
Comment #33
Alan Evans CreditAttribution: Alan Evans commentedI can't really fault this - it's a nice-looking piece of code that does what it sets out to do, so:
+1
Leaving the RTBC for image subsystem maintainers ...
Just a couple of thoughts which might improve it a bit:
Comment #34
OFF CreditAttribution: OFF commentedHow i can do it with d7?
Comment #35
rooby CreditAttribution: rooby commented@OFF:
I use the patch in comment #21.
Comment #36
j0rd CreditAttribution: j0rd commentedI need this patch for D7. Seems to be cropping my images at the bottom, which is not the best place to be cropping.
Comment #37
j0rd CreditAttribution: j0rd commentedRe-patched D7-dev. Fixed a problem with t() wrappers on the asserts.
Comment #38
fietserwinPlease, do not change version because that is what you have or want a patch for. First solve issues in the current development session, then backport it. BTW: As a backport would include UI text changes, I'm afraid this is a no go.
Comment #39
rlhawkFor anyone who needs this functionality in Drupal 7 and doesn't want to patch core: I've created a sandbox project that adds the anchor feature to the default Scale and Crop image effect.
Comment #40
andypostd8 patch needs re-roll
Comment #41
rlhawkHere's a re-roll of the patch for Drupal 8. The tests are almost certainly not correct, but I'm hoping someone else can take a look at them and modify as required.
Comment #42
rlhawkComment #44
alansaviolobo CreditAttribution: alansaviolobo commented41: scale_and_crop_anchor-1252606-40.patch queued for re-testing.
Comment #46
alansaviolobo CreditAttribution: alansaviolobo commentedComment #49
andypostFeatures goes to 8.2
Comment #50
mariiadeny CreditAttribution: mariiadeny commentedComment #51
mariiadeny CreditAttribution: mariiadeny as a volunteer and at Skilld commentedRerolled
Comment #52
andypostThis tag probably still makes sense
Comment #58
dsnopekHere's a first (untested) pass at re-rolling this for Drupal 8.6.x. Other than the changes necessary to make it work with a newer Drupal core, this also changes the approach slightly for backwards compatibility: rather than changing the function signature of
ImageInterface::scaleAndCrop()
it adds a newImageInterface::scaleAndCropWithAnchor()
. This could also be done with a new interface, but since no one will probably ever useImageInterface
withoutImage
, I think this is fine? If this extra work for BC isn't necessary, I'd be happy to switch this back to the old approach.Anyway, next up is actually testing that it works :-)
Comment #59
dsnopekHa! I just tried that locally and it actually worked. I wasn't expecting that :-)
Comment #61
dsnopekHere's a new patch to try and fix the schema errors.
Comment #63
dsnopekHere's an attempt to fix the other failures
Comment #64
dsnopekLooks like I missed one of the rest tests. Also, this patch adds coding style fixes
Comment #67
dsnopekHere's an updated patch which fixes the existing test for scale and crop (basically, the "BC mode"), and also adds a new test to test the new functionality added here. I'm hoping this one will actually pass!
Comment #68
rlhawkI tested the functionality and it worked exactly as expected.
Comment #69
dsnopek@rlhawk: Thanks for testing!
Reviewing the patch a little myself:
This is left over from an earlier version of the patch, but I don't think it's actually necessary. Should be removed.
I'm not sure if this is the right thing to do because I don't know the motivation for this test. Is the idea that Stable makes it's own version of every template in core? If so, we should probably copy the template into Stable.
The comment on the test is confusing it says "Ensures that Stable overrides all relevant core templates" - what makes a template "relevant"?
It has copies of all the other image effect summary templates, so I guess we should follow suite...
Comment #70
dsnopekHere's a new patch that fixes the things mentioned in #69. I can't think of anything else this patch needs - I think this is ready for wider review!
This issue could probably use an issue summary update, with the usual stuff from the template, but also explaining the backwards compatibility impact of this.
Comment #71
dsnopekUpdated the issue summary based on the template
Comment #72
dsnopekFix type-o and explain all the seemingly unrelated test changes in the issue summary
Comment #73
dsnopekAh, I did come up with one more thing to change. :-) Thinking about it more,
ImageInterface::scaleAndCropWithAnchor()
is a bad method name, because we're not passing in the anchor (that's in the image effect plugin), but the offset. So, this patch just renames that toImageInterface::scaleAndCropWithOffset()
. Updating the issue summary too.Comment #74
mondrakeIMHO, we should just avoid to make any change to
ImageInterface
andImage
. Just call$image->apply('scale_and_crop', ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height])
in the effect and that's all. There was a lot of discussion back in #2073759: Convert toolkit operations to plugins whether to have or not the manipulation methods in the Image class - exactly because the 'apply' method is general whereas hardcoded 'scale', 'crop' etc. methods will have the drawback of leading to BC break if any change needed... and here's the smoking gun in that sense. See the comment #280 there.See also #872206: Add possibility to not upscale for "Scale and crop" effect comments #152 and #153.
I also think we need an update function in order to update the effect configuration for existing image styles - see the patch in #872206-127: Add possibility to not upscale for "Scale and crop" effect for an example - that did not get in yet but was RTBC'ed at that point by the image system maintainer. NW for that.
Comment #75
dsnopekThanks, @mondrake!
That's a great idea! That avoids the most controversial part of this patch. +1!
I don't really understand why this is necessary since the default will be used if missing and it'll match existing behavior. But looking at the other issue, it was this comment where an update function was first requested by the image subsystem maintainer:
#872206-105: Add possibility to not upscale for "Scale and crop" effect
So, I guess, if the maintainer wants it, we gotta do it :-)
Comment #76
dsnopekHere's a new patch that removes the changes to
ImageInterface
andImage
, because that was the easier change to make. :-) I've updated the issue summary as well. The update hook will come later.Comment #77
sushylAdded patch for 8.4.x, noticed and removed some changes related to profile "demo_umami" which does not exist in 8.4.x
Comment #78
mondrake@sushyl feature requests are always targeted to the current development branch (8.6.x in this case). 8.4.x is unsupported now, so please do not change the version.
Comment #79
sushylMy bad!
Comment #80
yogeshmpawarSetting back to "Needs Review" state.
Comment #81
rlhawkHere's a new patch that adds a post-update hook, along with a test.
Comment #83
rlhawkIgnore that last patch. I will be uploading a new one, along with a a new interdiff.
Comment #84
rlhawkThis patch adds fixes for the failing test and the code standards issue. The interdiff is from drupal-scale-and-crop-anchor-1252606-76.patch in #76.
Comment #85
dsnopekThanks! Those changes look good to me. I'd RTBC, but I think I'm disqualified by working on earlier versions of the patch :-)
Comment #86
andypostLooks really good to go
Comment #87
alexpottWe no longer to do this.
There's a new scalable way to do this. We should always batch these type of updates as we have no idea how many image styles a site has. See #2949351: Add a helper class to make updating configuration simple
Comment #88
rlhawkThanks, @alexpott. That makes sense. Here's a new patch.
Comment #89
tstoecklerThanks @rlhawk! Some notes on the conversion:
$updated
variable we can simplyreturn TRUE;
inside the ifConfigEntityUpdater
will do that for us.Comment #90
rlhawkEven better. I'll update it.
Comment #91
rlhawkHere's an updated patch.
Comment #92
tstoecklerAwesome, looks perfect! Back to RTBC.
Comment #93
alexpottSorry I missed the fact that we should have a change record for this change - since the UI and config are changing.
Comment #94
rlhawkI'll write the change record.
Comment #95
rlhawkThe draft change record is here: https://www.drupal.org/node/2972030
Comment #96
mondrakeUhmm.. I think we also need to add
'x'
and'y'
arguments to the array in the::arguments()
method of theScaleAndCrop
image toolkit operation class. Currently, these are calculated in the::validateArguments
method, but with the change at hand they will be passed in by the image effect plugin.Contrib toolkits like ImageMagick will have to adjust, also, if they want to support the new arguments.
Comment #97
rlhawkHere's a patch with the 'x' and 'y' arguments added.
Comment #98
mondrakeThanks!
Comment #100
ashutosh.mishra CreditAttribution: ashutosh.mishra commentedthe patch is running. i tested in drupal 8.x cloned from github
Comment #101
rlhawkHere's a re-rolled patch.
Comment #102
rlhawkThe reroll was necessary because of #2910883: Move all entity type REST tests to the providing modules.
Comment #103
mondrakeComment #104
alexpottAdding review credit for patch reviews that affected the patch and explicit testing reviews.
Comment #105
alexpottCommitted bf86499 and pushed to 8.6.x. Thanks!
The backport policy now is that a new issue for D7 should be opened - see https://www.drupal.org/core/backport-policy
Comment #107
mondrake@alexpott I think this chmod went in by mistake
Comment #108
mondrakeFiled #2973804: Add crop anchor option to the Scale and Crop image toolkit operation to enable supporting the same in the ImageMagick module.
Comment #110
mondrakeSetting back to Fixed and sent @alexpott a message about #107.
Comment #113
sushyl@alexpott,
Do you think we could cherry pick this for 8.5.x?