diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 65679108..3156b310 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -12,7 +12,7 @@ parameters: - message: "#^Parameter \\#1 \\$data of function imagecreatefromstring expects string, string\\|false given\\.$#" - count: 2 + count: 1 path: src/Drivers/Gd/GdDriver.php - @@ -102,7 +102,7 @@ parameters: - message: "#^Property Spatie\\\\Image\\\\Drivers\\\\Gd\\\\GdDriver\\:\\:\\$image \\(GdImage\\) does not accept GdImage\\|false\\.$#" - count: 3 + count: 2 path: src/Drivers/Gd/GdDriver.php - diff --git a/src/Drivers/Gd/GdDriver.php b/src/Drivers/Gd/GdDriver.php index 66d3d66f..06f4d946 100644 --- a/src/Drivers/Gd/GdDriver.php +++ b/src/Drivers/Gd/GdDriver.php @@ -35,6 +35,8 @@ class GdDriver implements ImageDriver protected GdImage $image; + protected ?string $format = null; + /** @var array */ protected array $exif = []; @@ -131,9 +133,11 @@ public function save(?string $path = null): static if (! $path) { $path = $this->originalPath; } - - $extension = pathinfo($path, PATHINFO_EXTENSION); - + if (is_null($this->format)) { + $extension = pathinfo($path, PATHINFO_EXTENSION); + } else { + $extension = $this->format; + } switch (strtolower($extension)) { case 'jpg': case 'jpeg': @@ -158,6 +162,7 @@ public function save(?string $path = null): static if ($this->optimize) { $this->optimizerChain->optimize($path); } + $this->format = null; return $this; } @@ -166,7 +171,26 @@ public function base64(string $imageFormat = 'jpeg', bool $prefixWithFormat = tr { ob_start(); - $this->format($imageFormat); + switch (strtolower($imageFormat)) { + case 'jpg': + case 'jpeg': + imagejpeg($this->image, null, $this->quality); + break; + case 'png': + imagepng($this->image, null, $this->pngCompression()); + break; + case 'gif': + imagegif($this->image, null); + break; + case 'webp': + imagewebp($this->image, null); + break; + case 'avif': + imageavif($this->image, null); + break; + default: + throw UnsupportedImageFormat::make($imageFormat); + } $imageData = ob_get_contents(); ob_end_clean(); @@ -687,32 +711,10 @@ protected function pngCompression(): int public function format(string $format): static { - ob_start(); - - switch (strtolower($format)) { - case 'jpg': - case 'jpeg': - imagejpeg($this->image, null, $this->quality); - break; - case 'png': - imagepng($this->image, null, $this->pngCompression()); - break; - case 'gif': - imagegif($this->image, null); - break; - case 'webp': - imagewebp($this->image, null); - break; - case 'avif': - imageavif($this->image, null); - break; - default: - throw UnsupportedImageFormat::make($format); + if (! in_array($format, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif'])) { + throw UnsupportedImageFormat::make($format); } - - $this->image = imagecreatefromstring(ob_get_contents()); - - ob_end_clean(); + $this->format = $format; return $this; } diff --git a/tests/ImageFormatTest.php b/tests/ImageFormatTest.php index 86527360..a4446a56 100644 --- a/tests/ImageFormatTest.php +++ b/tests/ImageFormatTest.php @@ -25,7 +25,7 @@ return; } - $driver->loadFile(getTestJpg())->format($format); + $driver->loadFile(getTestJpg())->format($format)->save($this->tempDir->path("{$driver->driverName()}/format-test.$format")); })->with('drivers', ['jpeg', 'gif', 'png', 'webp', 'avif'])->throwsNoExceptions(); it('can save tiff', function () {