Skip to content

fix(modifiers): keep the trimmed area when a later corner matches it - #120

Merged
olivervogel merged 1 commit into
Intervention:developfrom
nlemoine:fix/trim-modifier-degenerate-trim-box
Aug 23, 2026
Merged

olivervogel merged 1 commit into
Intervention:developfrom
nlemoine:fix/trim-modifier-degenerate-trim-box

Conversation

@nlemoine

Copy link
Copy Markdown
Contributor

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().

ModifierException: Failed to trim image
  previous: Jcupitt\Vips\Exception: libvips error: extract_area: bad extract area

What the loop does on that image:

iter 0  bg=[255,255,255]  native=120x120  find_trim={50,50,70,70}  -> crop, native is 70x70 now
iter 1  bg=[255,255,255]  native=70x70    find_trim={0,0,70,70}    -> no-op
iter 2  bg=[255,255,255]  native=70x70    find_trim={0,0,70,70}    -> no-op
iter 3  bg=[255,0,0]      native=70x70    find_trim={70,70,0,0}    -> crop(70,70,1,1) on a 70x70

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_trim gets 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 === 0 check, 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, so crop(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 (imagecropauto returns false and the driver builds a 1x1) and imagick. I kept the 1x1 on purpose, testTrimHighTolerance asserts it, and there is a comment in the gd driver saying imagick does the same. The clamps are gone with it, find_trim always 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.

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.
@olivervogel

Copy link
Copy Markdown
Member

@nlemoine Thanks (again).

@olivervogel
olivervogel merged commit 2863529 into Intervention:develop Aug 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants