diff --git a/src/Drivers/Concerns/ValidatesArguments.php b/src/Drivers/Concerns/ValidatesArguments.php index 4dd0ef79..729e88ad 100644 --- a/src/Drivers/Concerns/ValidatesArguments.php +++ b/src/Drivers/Concerns/ValidatesArguments.php @@ -6,7 +6,7 @@ trait ValidatesArguments { - protected function ensureNumberBetween(int $value, int $min, int $max, string $label): void + protected function ensureNumberBetween(int|float $value, int|float $min, int|float $max, string $label): void { if ($value < $min || $value > $max) { throw InvalidManipulation::valueNotInRange($label, $value, $min, $max); diff --git a/src/Drivers/GdImageDriver.php b/src/Drivers/GdImageDriver.php index 369687b1..e903eb4d 100644 --- a/src/Drivers/GdImageDriver.php +++ b/src/Drivers/GdImageDriver.php @@ -245,4 +245,22 @@ public function resizeCanvas( return $this; } + + public function gamma(float $gamma): ImageDriver + { + $this->ensureNumberBetween($gamma, 0.1, 9.99, 'gamma'); + + imagegammacorrect($this->image, 1, $gamma); + + return $this; + } + + public function contrast(float $level): ImageDriver + { + $this->ensureNumberBetween($level, -100, 100, 'contrast'); + + imagefilter($this->image, IMG_FILTER_CONTRAST, ($level * -1)); + + return $this; + } } diff --git a/src/Drivers/ImagickImageDriver.php b/src/Drivers/ImagickImageDriver.php index 8e1a0784..78473f35 100644 --- a/src/Drivers/ImagickImageDriver.php +++ b/src/Drivers/ImagickImageDriver.php @@ -195,4 +195,22 @@ public function getSize(): Size { return new Size($this->getWidth(), $this->getHeight()); } + + public function gamma(float $gamma): ImageDriver + { + $this->ensureNumberBetween($gamma, 0.1, 9.99, 'gamma'); + + $this->image->gammaImage($gamma); + + return $this; + } + + public function contrast(float $level): ImageDriver + { + $this->ensureNumberBetween($level, -100, 100, 'contrast'); + + $this->image->brightnessContrastImage(1, $level); + + return $this; + } } diff --git a/tests/Manipulations/ContrastTest.php b/tests/Manipulations/ContrastTest.php new file mode 100644 index 00000000..dc968848 --- /dev/null +++ b/tests/Manipulations/ContrastTest.php @@ -0,0 +1,18 @@ +tempDir->path("{$driver->driverName()}/contrast.jpg"); + + $driver->load(getTestPhoto())->contrast(30)->save($targetFile); + + expect($targetFile)->toBeFile(); +})->with('drivers'); + +it('will throw an exception when passing an invalid contrast value', function (ImageDriver $driver) { + $driver->load(getTestPhoto())->brightness(101); +})->with('drivers')->throws(InvalidManipulation::class); diff --git a/tests/Manipulations/GammaTest.php b/tests/Manipulations/GammaTest.php new file mode 100644 index 00000000..63c72223 --- /dev/null +++ b/tests/Manipulations/GammaTest.php @@ -0,0 +1,13 @@ +tempDir->path("{$driver->driverName()}/gamma.jpg"); + + $driver->load(getTestJpg())->gamma(4.8)->save($targetFile); + + expect($targetFile)->toBeFile(); +})->with('drivers'); diff --git a/tests/Pest.php b/tests/Pest.php index e7ca0262..18aebc3b 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -21,6 +21,11 @@ function getTestJpg(): string return getTestFile('test.jpg'); } +function getTestPhoto(): string +{ + return getTestFile('test-photo.jpg'); +} + function getTestFile($fileName): string { return getTestSupportPath('testFiles/'.$fileName); diff --git a/tests/TestSupport/testFiles/test-photo.jpg b/tests/TestSupport/testFiles/test-photo.jpg new file mode 100644 index 00000000..7f7fd65c Binary files /dev/null and b/tests/TestSupport/testFiles/test-photo.jpg differ