mirror of
https://github.com/Intervention/image.git
synced 2025-08-01 11:30:16 +02:00
Add ColorizeModifiers
This commit is contained in:
@@ -138,6 +138,13 @@ abstract class AbstractImage implements ImageInterface
|
||||
);
|
||||
}
|
||||
|
||||
public function colorize(int $red = 0, int $green = 0, int $blue = 0): ImageInterface
|
||||
{
|
||||
return $this->modify(
|
||||
$this->resolveDriverClass('Modifiers\ColorizeModifier', $red, $green, $blue)
|
||||
);
|
||||
}
|
||||
|
||||
public function blur(int $amount = 5): ImageInterface
|
||||
{
|
||||
return $this->modify(
|
||||
|
31
src/Drivers/Gd/Modifiers/ColorizeModifier.php
Normal file
31
src/Drivers/Gd/Modifiers/ColorizeModifier.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
40
src/Drivers/Imagick/Modifiers/ColorizeModifier.php
Normal file
40
src/Drivers/Imagick/Modifiers/ColorizeModifier.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
@@ -218,6 +218,46 @@ interface ImageInterface extends Traversable, Countable
|
||||
*/
|
||||
public function greyscale(): ImageInterface;
|
||||
|
||||
/**
|
||||
* Invert the colors of the current image
|
||||
*
|
||||
* @return ImageInterface
|
||||
*/
|
||||
public function invert(): ImageInterface;
|
||||
|
||||
/**
|
||||
* Adjust brightness of the current image
|
||||
*
|
||||
* @param int $level
|
||||
* @return ImageInterface
|
||||
*/
|
||||
public function brightness(int $level): ImageInterface;
|
||||
|
||||
/**
|
||||
* Adjust contrast of the current image
|
||||
*
|
||||
* @param int $level
|
||||
* @return ImageInterface
|
||||
*/
|
||||
public function contrast(int $level): ImageInterface;
|
||||
|
||||
/**
|
||||
* Apply gamma correction on the current image
|
||||
*
|
||||
* @param float $gamma
|
||||
* @return ImageInterface
|
||||
*/
|
||||
public function gamma(float $gamma): ImageInterface;
|
||||
|
||||
/**
|
||||
* Adjust the intensity of the RGB color channels
|
||||
*
|
||||
* @param int $red
|
||||
* @param int $green
|
||||
* @param int $blue
|
||||
* @return ImageInterface
|
||||
*/
|
||||
public function colorize(int $red = 0, int $green = 0, int $blue = 0): ImageInterface;
|
||||
|
||||
/**
|
||||
* Blur current image by given strength
|
||||
|
24
tests/Drivers/Gd/Modifiers/ColorizeModifierTest.php
Normal file
24
tests/Drivers/Gd/Modifiers/ColorizeModifierTest.php
Normal file
@@ -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));
|
||||
}
|
||||
}
|
24
tests/Drivers/Imagick/Modifiers/ColorizeModifierTest.php
Normal file
24
tests/Drivers/Imagick/Modifiers/ColorizeModifierTest.php
Normal file
@@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user