Skip to content
Open
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
16 changes: 13 additions & 3 deletions src/Drivers/Imagick/Modifiers/GrayscaleModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Drivers\Imagick\Modifiers;

use Imagick;
use ImagickException;
use Intervention\Image\Exceptions\ModifierException;
use Intervention\Image\Interfaces\ImageInterface;
Expand All @@ -19,15 +20,24 @@ public function apply(ImageInterface $image): ImageInterface
{
foreach ($image as $frame) {
try {
$result = $frame->native()->modulateImage(100, 0, 100);
// turn image to grayscale
$result = $frame->native()->transformImageColorspace(Imagick::COLORSPACE_GRAY);
if ($result === false) {
throw new ModifierException(
'Failed to apply ' . self::class . ', unable to modulate image',
'Failed to apply ' . self::class . ', unable to transform image to grayscale',
);
}

// return to srgb colorspace with grayscale image
$result = $frame->native()->transformImageColorspace(Imagick::COLORSPACE_SRGB);
if ($result === false) {
throw new ModifierException(
'Failed to apply ' . self::class . ', unable to transform image to grayscale',
);
}
} catch (ImagickException $e) {
throw new ModifierException(
'Failed to apply ' . self::class . ', unable to modulate image',
'Failed to apply ' . self::class . ', unable to transform image to grayscale',
previous: $e,
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unit/Drivers/Gd/Modifiers/GrayscaleModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Modifiers\GrayscaleModifier;
use Intervention\Image\Tests\GdTestCase;

Expand All @@ -21,4 +22,18 @@ public function testColorChange(): void
$image->modify(new GrayscaleModifier());
$this->assertTrue($image->colorAt(0, 0)->isGrayscale());
}

public function testSaturatedColorsKeepDistinctBrightness(): void
{
// blocks.png holds pure blue, green and red in three of its quadrants
$image = $this->readTestImage('blocks.png')->modify(new GrayscaleModifier());

$blue = $image->colorAt(160, 120)->channel(Red::class)->value();
$green = $image->colorAt(160, 360)->channel(Red::class)->value();
$red = $image->colorAt(480, 360)->channel(Red::class)->value();

// any luma based conversion keeps blue darkest and green brightest
$this->assertLessThan($red, $blue);
$this->assertLessThan($green, $red);
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Drivers/Imagick/Modifiers/GrayscaleModifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Modifiers\GrayscaleModifier;
use Intervention\Image\Tests\ImagickTestCase;

Expand All @@ -21,4 +22,18 @@ public function testColorChange(): void
$image->modify(new GrayscaleModifier());
$this->assertTrue($image->colorAt(0, 0)->isGrayscale());
}

public function testSaturatedColorsKeepDistinctBrightness(): void
{
// blocks.png holds pure blue, green and red in three of its quadrants
$image = $this->readTestImage('blocks.png')->modify(new GrayscaleModifier());

$blue = $image->colorAt(160, 120)->channel(Red::class)->value();
$green = $image->colorAt(160, 360)->channel(Red::class)->value();
$red = $image->colorAt(480, 360)->channel(Red::class)->value();

// any luma based conversion keeps blue darkest and green brightest
$this->assertLessThan($red, $blue);
$this->assertLessThan($green, $red);
}
}