Skip to content

Commit

Permalink
Add sharpen method.
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandijck committed Sep 11, 2023
1 parent 1ed290a commit 115a89a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Drivers/Gd/GdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,23 @@ public function sepia(): ImageDriver
->colorize(38, 25, 10)
->contrast(5);
}

public function sharpen(float $amount): self
{
$this->ensureNumberBetween($amount, 0, 100, 'sharpen');

$min = $amount >= 10 ? $amount * -0.01 : 0;
$max = $amount * -0.025;
$abs = ((4 * $min + 4 * $max) * -1) + 1;

$matrix = [
[$min, $max, $min],
[$max, $abs, $max],
[$min, $max, $min]
];

imageconvolution($this->image, $matrix, 1, 0);

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Drivers/ImageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function greyscale(): self;

public function sepia(): self;

public function sharpen(float $amount): self;

public function getSize(): Size;

public function fit(Fit $fit, int $desiredWidth = null, int $desiredHeight = null): self;
Expand Down
9 changes: 9 additions & 0 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,13 @@ public function sepia(): ImageDriver
->brightness(-10)
->contrast(10);
}

public function sharpen(float $amount): self
{
$this->ensureNumberBetween($amount, 0, 100, 'sharpen');

$this->image->unsharpMaskImage(1, 1, $amount / 6.25, 0);

return $this;
}
}
16 changes: 16 additions & 0 deletions tests/Manipulations/SharpenTest.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 sharpen an image', function (ImageDriver $driver) {
$targetFile = $this->tempDir->path("{$driver->driverName()}/sharpen.jpg");

$driver->load(getTestJpg())->sharpen(50)->save($targetFile);

expect($targetFile)->toBeFile();
})->with('drivers');

it('will throw an exception when passing an invalid sharpen value', function (ImageDriver $driver) {
$driver->load(getTestJpg())->sharpen(101);
})->with('drivers')->throws(InvalidManipulation::class);

0 comments on commit 115a89a

Please sign in to comment.