Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in ImagickDriver::pixelate that caused a DivideByZero excep… #262

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v2.1.0
uses: dependabot/fetch-metadata@v2.2.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
compat-lookup: true
Expand Down
40 changes: 23 additions & 17 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Spatie\Image\Drivers\Imagick;

use Imagick;
Expand Down Expand Up @@ -56,13 +58,6 @@ public function new(int $width, int $height, ?string $backgroundColor = null): s
return (new self())->setImage($image);
}

protected function setImage(Imagick $image): static
{
$this->image = $image;

return $this;
}

public function image(): Imagick
{
return $this->image;
Expand All @@ -88,11 +83,6 @@ public function loadFile(string $path, bool $autoRotate = true): static
return $this;
}

protected function isAnimated(): bool
{
return count($this->image) > 1;
}

public function getWidth(): int
{
return $this->image->getImageWidth();
Expand Down Expand Up @@ -133,7 +123,7 @@ public function fit(
}

if ($fit === Fit::FillMax) {
if (is_null($desiredWidth) || is_null($desiredHeight)) {
if (null === $desiredWidth || null === $desiredHeight) {
throw new MissingParameter('Both desiredWidth and desiredHeight must be set when using Fit::FillMax');
}

Expand Down Expand Up @@ -223,7 +213,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 @@ -265,7 +255,7 @@ public function save(?string $path = null): static
$formats[] = 'JFIF';
}

if (! in_array(strtoupper($extension), $formats)) {
if (! in_array(mb_strtoupper($extension), $formats)) {
throw UnsupportedImageFormat::make($extension);
}

Expand Down Expand Up @@ -354,7 +344,7 @@ public function manualCrop(int $width, int $height, ?int $x = null, ?int $y = nu
$cropped = new Size($width, $height);
$position = new Point($x ?? 0, $y ?? 0);

if (is_null($x) && is_null($y)) {
if (null === $x && null === $y) {
$position = $this
->getSize()
->align(AlignPosition::Center)
Expand Down Expand Up @@ -429,7 +419,7 @@ public function overlay(ImageDriver $bottomImage, ImageDriver $topImage, int $x

public function orientation(?Orientation $orientation = null): static
{
if (is_null($orientation)) {
if (null === $orientation) {
$orientation = $this->getOrientationFromExif($this->exif);
}

Expand Down Expand Up @@ -470,6 +460,10 @@ public function flip(FlipDirection $flip): static

public function pixelate(int $pixelate = 50): static
{
if ($pixelate === 0) {
return $this;
}

$width = $this->getWidth();
$height = $this->getHeight();

Expand Down Expand Up @@ -722,4 +716,16 @@ public function wrapText(string $text, int $fontSize, string $fontPath = '', int

return $wrapped;
}

protected function setImage(Imagick $image): static
{
$this->image = $image;

return $this;
}

protected function isAnimated(): bool
{
return count($this->image) > 1;
}
}
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
13 changes: 10 additions & 3 deletions tests/Manipulations/PixelateTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<?php

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

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.