Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Modifiers/TrimModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function apply(ImageInterface $image): ImageInterface

$core = Core::ensureInMemory($image->core());
$native = $core->native();
$width = $native->width;
$height = $native->height;
$maxThreshold = match ($image->core()->native()->format) {
BandFormat::USHORT => 65535,
BandFormat::FLOAT => 1,
Expand All @@ -52,16 +54,23 @@ public function apply(ImageInterface $image): ImageInterface
'background' => $point,
]);

$native = $native->crop(
min($trim['left'], $image->width() - 1),
min($trim['top'], $image->height() - 1),
max($trim['width'], 1),
max($trim['height'], 1),
);

// An empty box means the whole remaining image matches the background color of
// the current corner. The corner colors are read from the original image, so a
// later corner may well match an area that an earlier corner has already
// isolated. That area is content and must be kept.
if ($trim['width'] === 0 || $trim['height'] === 0) {
if ($native->width < $width || $native->height < $height) {
continue;
}

// Nothing but background left. Return a single pixel, which is what the
// other drivers do in this case.
$native = $native->crop(0, 0, 1, 1);

break;
}

$native = $native->crop($trim['left'], $trim['top'], $trim['width'], $trim['height']);
} catch (VipsException $e) {
throw new ModifierException('Failed to trim image', previous: $e);
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/Modifiers/TrimModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

namespace Intervention\Image\Drivers\Vips\Tests\Unit\Modifiers;

use Intervention\Image\Drivers\Vips\Driver;
use Intervention\Image\Drivers\Vips\Modifiers\TrimModifier;
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Modifiers\DrawRectangleModifier;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(TrimModifier::class)]
Expand Down Expand Up @@ -56,4 +62,36 @@ public function testTrimAnimated(): void
$this->expectException(NotSupportedException::class);
$image->modify(new TrimModifier());
}

public function testTrimWithDifferentCornerColors(): void
{
$image = $this->cornerTestImage();
$this->assertEquals('ffffff', $image->colorAt(0, 0)->toHex());
$this->assertEquals('ff0000', $image->colorAt(119, 119)->toHex());

$image->modify(new TrimModifier());

$this->assertEquals(70, $image->width());
$this->assertEquals(70, $image->height());
$this->assertEquals('ff0000', $image->colorAt(0, 0)->toHex());
}

/**
* Create a 120x120 image with a white border area and a red block in the
* lower right corner, so that three corners are white and one is red.
*/
private function cornerTestImage(): ImageInterface
{
$image = ImageManager::usingDriver(Driver::class)->createImage(120, 120);

$background = new Rectangle(120, 120, new Point(0, 0));
$background->setBackgroundColor('ffffff');
$image->modify(new DrawRectangleModifier($background));

$block = new Rectangle(70, 70, new Point(50, 50));
$block->setBackgroundColor('ff0000');
$image->modify(new DrawRectangleModifier($block));

return $image;
}
}