Repository navigation
fix(modifiers): keep the trimmed area when a later corner matches it - #120
Merged
olivervogel merged 1 commit intoAug 23, 2026
Conversation
TrimModifier reads the four corner colors of the original image, then loops
find_trim/crop cumulatively on the same native. Once an earlier corner has
trimmed something, a later corner color can match the whole remaining area,
and find_trim then returns an empty box pinned to the far corner, for
instance `{left: 70, top: 70, width: 0, height: 0}` on a 70x70 native.
That box was cropped before it was checked, and the crop clamped against
the original image size instead of the current native size, so the clamp
saved nothing and libvips threw `extract_area: bad extract area`, wrapped
as `Failed to trim image`. A 120x120 image with three white corners and one
red corner reproduces it, the red block survives the first corner and then
matches the fourth one whole.
Handle the empty box before the crop instead. If an earlier corner already
trimmed something, the remaining area is content that the corner colors of
the original image no longer describe, so skip that corner. If nothing was
trimmed, the image is entirely background and a single pixel is returned,
which is what the gd and imagick drivers do in the same situation. The
clamps are gone with it, find_trim always returns a box that lies inside
the native it was measured on.
Adds a regression test for the disagreeing corners, which now trims to the
70x70 block. The existing high tolerance test covers the other arm, an
image that is background everywhere still ends up 1x1.
Member
|
@nlemoine Thanks (again). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
trim()throws instead of trimming when the four corner pixels are not the same color.Repro: a 120x120 image, white on the top 50 rows and the left 50 columns, red everywhere else. Three corners white, one red. Then
->trim().What the loop does on that image:
The corner colors are read once on the original image, but the crops are cumulative. By the fourth corner the native is all red, so
find_trimgets asked to trim red out of an all red image. It returns an empty box at the far corner,{left: 70, top: 70, width: 0, height: 0}.Two things go wrong then. The crop runs before the
width === 0check, so the empty box gets used before it is detected. And the clamp reads$image->width(), which is still 119 because it is never refreshed after the earlier crops. Wrong canvas, socrop(70, 70, 1, 1)goes through on a 70x70 native.So I check the empty box before the crop instead. If an earlier corner already trimmed something, what is left is content and the original corner colors do not describe it anymore, so I skip that corner. If nothing was trimmed yet, the image is background everywhere and I return a 1x1, like gd does (
imagecropautoreturns false and the driver builds a 1x1) and imagick. I kept the 1x1 on purpose,testTrimHighToleranceasserts it, and there is a comment in the gd driver saying imagick does the same. The clamps are gone with it,find_trimalways returns a box inside the native it measured.On the repro the driver returns 70x70 now, same as imagick. gd returns 120x120, that is its corner averaging.
Adds a regression test for the disagreeing corners. Tested with libvips 8.18.5, ext vips 1.0.13, PHP 8.3.