-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5f141f
commit 2c5af00
Showing
6 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Drivers\Gd\Modifiers; | ||
|
||
use Intervention\Image\Interfaces\ImageInterface; | ||
use Intervention\Image\Interfaces\ModifierInterface; | ||
|
||
class ColorizeModifier implements ModifierInterface | ||
{ | ||
public function __construct( | ||
protected int $red = 0, | ||
protected int $green = 0, | ||
protected int $blue = 0 | ||
) { | ||
// | ||
} | ||
|
||
public function apply(ImageInterface $image): ImageInterface | ||
{ | ||
// normalize colorize levels | ||
$red = round($this->red * 2.55); | ||
$green = round($this->green * 2.55); | ||
$blue = round($this->blue * 2.55); | ||
|
||
foreach ($image as $frame) { | ||
imagefilter($frame->core(), IMG_FILTER_COLORIZE, $red, $green, $blue); | ||
} | ||
|
||
return $image; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Drivers\Imagick\Modifiers; | ||
|
||
use Imagick; | ||
use Intervention\Image\Interfaces\ImageInterface; | ||
use Intervention\Image\Interfaces\ModifierInterface; | ||
|
||
class ColorizeModifier implements ModifierInterface | ||
{ | ||
public function __construct( | ||
protected int $red = 0, | ||
protected int $green = 0, | ||
protected int $blue = 0 | ||
) { | ||
// | ||
} | ||
|
||
public function apply(ImageInterface $image): ImageInterface | ||
{ | ||
// normalize colorize levels | ||
$red = $this->normalizeLevel($this->red); | ||
$green = $this->normalizeLevel($this->green); | ||
$blue = $this->normalizeLevel($this->blue); | ||
|
||
foreach ($image as $frame) { | ||
$qrange = $frame->core()->getQuantumRange(); | ||
$frame->core()->levelImage(0, $red, $qrange['quantumRangeLong'], Imagick::CHANNEL_RED); | ||
$frame->core()->levelImage(0, $green, $qrange['quantumRangeLong'], Imagick::CHANNEL_GREEN); | ||
$frame->core()->levelImage(0, $blue, $qrange['quantumRangeLong'], Imagick::CHANNEL_BLUE); | ||
} | ||
|
||
return $image; | ||
} | ||
|
||
private function normalizeLevel(int $level): int | ||
{ | ||
return $level > 0 ? $level / 5 : ($level + 100) / 100; | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Tests\Drivers\Gd\Modifiers; | ||
|
||
use Intervention\Image\Drivers\Gd\Modifiers\ColorizeModifier; | ||
use Intervention\Image\Tests\TestCase; | ||
use Intervention\Image\Tests\Traits\CanCreateGdTestImage; | ||
|
||
/** | ||
* @requires extension gd | ||
* @covers \Intervention\Image\Drivers\Gd\Modifiers\ColorizeModifier | ||
*/ | ||
class ColorizeModifierTest extends TestCase | ||
{ | ||
use CanCreateGdTestImage; | ||
|
||
public function testModify(): void | ||
{ | ||
$image = $this->createTestImage('tile.png'); | ||
$image = $image->modify(new ColorizeModifier(100, -100, -100)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(5, 5)); | ||
$this->assertColor(255, 0, 0, 255, $image->pickColor(15, 15)); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Intervention\Image\Tests\Drivers\Imagick\Modifiers; | ||
|
||
use Intervention\Image\Drivers\Imagick\Modifiers\ColorizeModifier; | ||
use Intervention\Image\Tests\TestCase; | ||
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage; | ||
|
||
/** | ||
* @requires extension gd | ||
* @covers \Intervention\Image\Drivers\Imagick\Modifiers\ColorizeModifier | ||
*/ | ||
class ColorizeModifierTest extends TestCase | ||
{ | ||
use CanCreateImagickTestImage; | ||
|
||
public function testModify(): void | ||
{ | ||
$image = $this->createTestImage('tile.png'); | ||
$image = $image->modify(new ColorizeModifier(100, -100, -100)); | ||
$this->assertColor(251, 0, 0, 255, $image->pickColor(5, 5)); | ||
$this->assertColor(239, 0, 0, 255, $image->pickColor(15, 15)); | ||
} | ||
} |