Skip to content

Commit

Permalink
Add ColorizeModifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 31, 2023
1 parent e5f141f commit 2c5af00
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Drivers/Abstract/AbstractImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public function gamma(float $gamma): 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(
Expand Down
31 changes: 31 additions & 0 deletions src/Drivers/Gd/Modifiers/ColorizeModifier.php
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;
}
}
40 changes: 40 additions & 0 deletions src/Drivers/Imagick/Modifiers/ColorizeModifier.php
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;
}
}
40 changes: 40 additions & 0 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,46 @@ public function text(string $text, int $x, int $y, ?callable $init = null): Imag
*/
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
Expand Down
24 changes: 24 additions & 0 deletions tests/Drivers/Gd/Modifiers/ColorizeModifierTest.php
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));
}
}
24 changes: 24 additions & 0 deletions tests/Drivers/Imagick/Modifiers/ColorizeModifierTest.php
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));
}
}

0 comments on commit 2c5af00

Please sign in to comment.