Skip to content

Commit

Permalink
Fix a bug in ImagickDriver::pixelate that caused a DivideByZero excep…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
psion-ar committed Jun 30, 2024
1 parent c15fa28 commit 0687d1b
Show file tree
Hide file tree
Showing 30 changed files with 23 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function resizeCanvas(

// make image area transparent to keep transparency
// even if background-color is set
$rect = new ImagickDraw;
$rect = new ImagickDraw();
$fill = $canvas->pickColor(0, 0, ColorFormat::Hex);
$fill = $fill === '#ff0000' ? '#00ff00' : '#ff0000';
$rect->setFillColor($fill);
Expand Down Expand Up @@ -470,12 +470,14 @@ public function flip(FlipDirection $flip): static

public function pixelate(int $pixelate = 50): static
{
$width = $this->getWidth();
$height = $this->getHeight();
if ($pixelate !== 0) {
$width = $this->getWidth();
$height = $this->getHeight();

foreach ($this->image as $image) {
$image->scaleImage(max(1, (int) ($width / $pixelate)), max(1, (int) ($height / $pixelate)));
$image->scaleImage($width, $height);
foreach ($this->image as $image) {
$image->scaleImage(max(1, (int) ($width / $pixelate)), max(1, (int) ($height / $pixelate)));
$image->scaleImage($width, $height);
}
}

return $this;
Expand Down
6 changes: 3 additions & 3 deletions tests/Manipulations/BlurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use function Spatie\Snapshots\assertMatchesImageSnapshot;

it('can blur an image', function (ImageDriver $driver) {
it('can blur an image', function (ImageDriver $driver, int $blur) {
$targetFile = $this->tempDir->path("{$driver->driverName()}/blur.png");

$driver->loadFile(getTestJpg())->blur(50)->save($targetFile);
$driver->loadFile(getTestJpg())->blur($blur)->save($targetFile);

assertMatchesImageSnapshot($targetFile);
})->with('drivers');
})->with('drivers', [0, 50, 100]);

it('will throw an exception when passing an invalid blur value', function (ImageDriver $driver) {
$driver->loadFile(getTestJpg())->blur(101);
Expand Down
12 changes: 9 additions & 3 deletions tests/Manipulations/PixelateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

use Spatie\Image\Drivers\ImageDriver;

use Spatie\Image\Exceptions\InvalidManipulation;

use function Spatie\Snapshots\assertMatchesImageSnapshot;

it('can pixelate an image', function (ImageDriver $driver) {
it('can pixelate an image', function (ImageDriver $driver, int $pixelate) {
$targetFile = $this->tempDir->path("{$driver->driverName()}/pixelate.png");

$driver->loadFile(getTestJpg())->pixelate()->save($targetFile);
$driver->loadFile(getTestJpg())->pixelate($pixelate)->save($targetFile);

assertMatchesImageSnapshot($targetFile);
})->with('drivers');
})->with('drivers', [0, 50, 100]);

it('will throw an exception when passing an invalid pixelate value', function (ImageDriver $driver) {
$driver->loadFile(getTestJpg())->pixelate(101);
})->with('drivers')->throws(InvalidManipulation::class);
6 changes: 3 additions & 3 deletions tests/Manipulations/SharpenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use function Spatie\Snapshots\assertMatchesImageSnapshot;

it('can sharpen an image', function (ImageDriver $driver) {
it('can sharpen an image', function (ImageDriver $driver, int $sharpen) {
$targetFile = $this->tempDir->path("{$driver->driverName()}/sharpen.png");

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

assertMatchesImageSnapshot($targetFile);
})->with('drivers');
})->with('drivers', [0, 50, 100]);

it('will throw an exception when passing an invalid sharpen value', function (ImageDriver $driver) {
$driver->loadFile(getTestJpg())->sharpen(101);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0687d1b

Please sign in to comment.