Skip to content

Commit

Permalink
Add colorize logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandijck committed Sep 8, 2023
1 parent 6200761 commit cbf3f71
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Drivers/Gd/GdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -341,4 +356,9 @@ public function focalCrop(int $width, int $height, $cropCenterX = null, $cropCen

return $this;
}

public function sepia(): ImageDriver
{
// TODO: Implement sepia() method.
}
}
4 changes: 4 additions & 0 deletions src/Drivers/ImageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/Drivers/Imagick/Helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\Image\Drivers\Imagick;

class Helpers
{
public static function normalizeColorizeLevel(float $level): float
{
if ($level > 0) {
return $level/5;
} else {
return ($level+100)/100;
}
}
}
25 changes: 25 additions & 0 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -262,4 +282,9 @@ public function focalCrop(int $width, int $height, $cropCenterX = null, $cropCen

return $this;
}

public function sepia(): ImageDriver
{
// TODO: Implement sepia() method.
}
}
16 changes: 16 additions & 0 deletions tests/Manipulations/ColorizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Spatie\Image\Drivers\ImageDriver;
use Spatie\Image\Exceptions\InvalidManipulation;

it('can blur an image', function (ImageDriver $driver) {
$targetFile = $this->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);

0 comments on commit cbf3f71

Please sign in to comment.