-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add settings for symmetric .trim() #4087
Labels
Comments
This looks highly specific to a particular scenario and feels like it belongs in a consumer of sharp rather than sharp itself. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feature request
Currently the
.trim()
method trims each border as much as possible. If the image is asymmetric then the trimOffsets on each axis might differ (e.g.trimOffsetLeft
!==trimOffsetRight
ortrimOffsetTop
!==trimOffsetBottom
).In my use case, I wish to keep the image positioning the same when I center the new image over a larger canvas. If the image was asymmetric (and therefore so was the trim) it will be offset by the difference in the trimOffsets.
I wish for the option for symmetric trimming, which would trim the chosen axis only by the lesser of both sides' lengths.
Current workaround is to first calculate the
trimOffsetRight
andtrimOffsetBottom
which are not provided on the info object (see #4086). Then calculate the x-axis and y-axis difference in offsets. After getting these difference numbers, you have to re-add the "missing" border to the correct side, which was trimmed in excess.This needs to happen before resizing, which forces you to save to buffer first, and then again read that new buffer into a new sharp instance for further processing.
Ideally, I propose 3 additional settings for
.trim()
, which would allow symmetric trimming:symmetric: boolean
: symmetric trim on both x-axis & y-axisxSymmetric: boolean
: symmetric trim on x-axisySymmetric: boolean
: symmetric trim on y-axisAs proposed, it should trim the same amount on both sides of the axis, equal to the lesser of the two available sides.
See also: #4086
The text was updated successfully, but these errors were encountered: