Skip to content

Commit

Permalink
Match trim from gd/imagick (#49)
Browse files Browse the repository at this point in the history
* match trim from gd/imagick

* add default to constructor
  • Loading branch information
deluxetom authored Jan 6, 2025
1 parent a0b1ea6 commit 0049223
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/Modifiers/TrimModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@

namespace Intervention\Image\Drivers\Vips\Modifiers;

use Intervention\Image\Drivers\SpecializableModifier;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\TrimModifier as GenericTrimModifier;
use Jcupitt\Vips\BandFormat;

class TrimModifier extends GenericTrimModifier implements SpecializedInterface
class TrimModifier extends SpecializableModifier implements SpecializedInterface
{
public function __construct(public int $tolerance = 40)
{
//
}

/**
* {@inheritdoc}
*
* @see ModifierInterface::apply()
*/
public function apply(ImageInterface $image): ImageInterface
{
if ($image->isAnimated()) {
Expand All @@ -20,11 +30,6 @@ public function apply(ImageInterface $image): ImageInterface

$core = $image->core()->native();

if ($image->core()->native()->hasAlpha()) {
// extract alpha channel
$core = $image->core()->native()->extract_band($image->core()->native()->bands - 1, ['n' => 1]);
}

// get the color of the 4 corners
$points = [
$core->getpoint(0, 0),
Expand All @@ -34,23 +39,20 @@ public function apply(ImageInterface $image): ImageInterface
];

$maxThreshold = match ($image->core()->native()->format) {
BandFormat::UCHAR => 255,
BandFormat::USHORT => 65535,
BandFormat::FLOAT => 1,
default => 255,
};

foreach ($points as $point) {
unset($point[3]); // remove alpha

$trim = $core->find_trim([
'threshold' => min($this->tolerance, $maxThreshold),
'threshold' => min($this->tolerance + 10, $maxThreshold),
'background' => $point,
]);

$core = $core->crop(
min($trim['top'], $image->height() - 1),
min($trim['left'], $image->width() - 1),
min($trim['top'], $image->height() - 1),
max($trim['width'], 1),
max($trim['height'], 1)
);
Expand Down
13 changes: 10 additions & 3 deletions tests/Unit/Modifiers/TrimModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

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

use Intervention\Image\Drivers\Vips\Modifiers\TrimModifier;
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Modifiers\TrimModifier;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(\Intervention\Image\Modifiers\TrimModifier::class)]
#[CoversClass(\Intervention\Image\Drivers\Vips\Modifiers\TrimModifier::class)]
#[CoversClass(TrimModifier::class)]
final class TrimModifierTest extends BaseTestCase
{
public function testTrim(): void
Expand All @@ -23,6 +22,14 @@ public function testTrim(): void
$this->assertEquals(28, $image->height());
}

public function testTrimApple(): void
{
$image = $this->readTestImage('apple.jpg');
$image->modify(new TrimModifier());
$this->assertEquals(81, $image->width());
$this->assertEquals(91, $image->height());
}

public function testTrimGradient(): void
{
$image = $this->readTestImage('radial.png');
Expand Down
Binary file added tests/resources/apple.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0049223

Please sign in to comment.