From cbf3f712326fe411f1ab46003d38c25347283146 Mon Sep 17 00:00:00 2001 From: Tim Van Dijck Date: Fri, 8 Sep 2023 16:08:15 +0200 Subject: [PATCH] Add colorize logic. --- src/Drivers/Gd/GdDriver.php | 20 ++++++++++++++++++++ src/Drivers/ImageDriver.php | 4 ++++ src/Drivers/Imagick/Helpers.php | 15 +++++++++++++++ src/Drivers/Imagick/ImagickDriver.php | 25 +++++++++++++++++++++++++ tests/Manipulations/ColorizeTest.php | 16 ++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 src/Drivers/Imagick/Helpers.php create mode 100644 tests/Manipulations/ColorizeTest.php diff --git a/src/Drivers/Gd/GdDriver.php b/src/Drivers/Gd/GdDriver.php index 65ba12a6..a5662d63 100644 --- a/src/Drivers/Gd/GdDriver.php +++ b/src/Drivers/Gd/GdDriver.php @@ -289,6 +289,21 @@ public function contrast(float $level): self return $this; } + public function colorize(int $red, int $green, int $blue): ImageDriver + { + $this->ensureNumberBetween($red, -100, 100, 'red'); + $this->ensureNumberBetween($green, -100, 100, 'green'); + $this->ensureNumberBetween($blue, -100, 100, 'blue'); + + $red = round($red * 2.55); + $green = round($green * 2.55); + $blue = round($blue * 2.55); + + imagefilter($this->image, IMG_FILTER_COLORIZE, $red, $green, $blue); + + return $this; + } + public function manualCrop(int $width, int $height, int $x = null, int $y = null): self { $cropped = new Size($width, $height); @@ -341,4 +356,9 @@ public function focalCrop(int $width, int $height, $cropCenterX = null, $cropCen return $this; } + + public function sepia(): ImageDriver + { + // TODO: Implement sepia() method. + } } diff --git a/src/Drivers/ImageDriver.php b/src/Drivers/ImageDriver.php index e8a4ff42..ba07f4a9 100644 --- a/src/Drivers/ImageDriver.php +++ b/src/Drivers/ImageDriver.php @@ -36,6 +36,10 @@ public function contrast(float $level): self; */ public function blur(int $blur): self; + public function colorize(int $red, int $green, int $blue): self; + + public function sepia(): self; + public function getSize(): Size; public function fit(Fit $fit, int $desiredWidth = null, int $desiredHeight = null): self; diff --git a/src/Drivers/Imagick/Helpers.php b/src/Drivers/Imagick/Helpers.php new file mode 100644 index 00000000..9fb3a8a0 --- /dev/null +++ b/src/Drivers/Imagick/Helpers.php @@ -0,0 +1,15 @@ + 0) { + return $level/5; + } else { + return ($level+100)/100; + } + } +} diff --git a/src/Drivers/Imagick/ImagickDriver.php b/src/Drivers/Imagick/ImagickDriver.php index 441adda0..2c2c9ebc 100644 --- a/src/Drivers/Imagick/ImagickDriver.php +++ b/src/Drivers/Imagick/ImagickDriver.php @@ -4,6 +4,7 @@ use Imagick; use ImagickDraw; +use ImagickPixel; use Spatie\Image\Drivers\Concerns\CalculatesCropOffsets; use Spatie\Image\Drivers\Concerns\CalculatesFocalCropCoordinates; use Spatie\Image\Drivers\Concerns\ValidatesArguments; @@ -224,6 +225,25 @@ public function contrast(float $level): self return $this; } + public function colorize(int $red, int $green, int $blue): ImageDriver + { + $this->ensureNumberBetween($red, -100, 100, 'red'); + $this->ensureNumberBetween($green, -100, 100, 'green'); + $this->ensureNumberBetween($blue, -100, 100, 'blue'); + + $quantumRange = $this->image->getQuantumRange(); + + $red = Helpers::normalizeColorizeLevel($red); + $green = Helpers::normalizeColorizeLevel($green); + $blue = Helpers::normalizeColorizeLevel($blue); + + $this->image->levelImage(0, $red, $quantumRange['quantumRangeLong'], Imagick::CHANNEL_RED); + $this->image->levelImage(0, $green, $quantumRange['quantumRangeLong'], Imagick::CHANNEL_GREEN); + $this->image->levelImage(0, $blue, $quantumRange['quantumRangeLong'], Imagick::CHANNEL_BLUE); + + return $this; + } + public function manualCrop(int $width, int $height, int $x = null, int $y = null): self { $cropped = new Size($width, $height); @@ -262,4 +282,9 @@ public function focalCrop(int $width, int $height, $cropCenterX = null, $cropCen return $this; } + + public function sepia(): ImageDriver + { + // TODO: Implement sepia() method. + } } diff --git a/tests/Manipulations/ColorizeTest.php b/tests/Manipulations/ColorizeTest.php new file mode 100644 index 00000000..58ad105d --- /dev/null +++ b/tests/Manipulations/ColorizeTest.php @@ -0,0 +1,16 @@ +tempDir->path("{$driver->driverName()}/blur.jpg"); + + $driver->load(getTestJpg())->colorize(10, 70, 10)->save($targetFile); + + expect($targetFile)->toBeFile(); +})->with('drivers'); + +it('will throw an exception when passing an invalid blur value', function (ImageDriver $driver) { + $driver->load(getTestJpg())->colorize(101, 100, 100); +})->with('drivers')->throws(InvalidManipulation::class);