Skip to content

Commit

Permalink
update GD driver (#221)
Browse files Browse the repository at this point in the history
* update GD driver

* Fix styling

* fix phpstan

* update base64 file

* fix tests

* Fix styling

* wip

---------

Co-authored-by: ariaieboy <[email protected]>
  • Loading branch information
ariaieboy and ariaieboy committed Dec 22, 2023
1 parent 78f7678 commit cf72c68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

-
Expand Down Expand Up @@ -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

-
Expand Down
60 changes: 31 additions & 29 deletions src/Drivers/Gd/GdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class GdDriver implements ImageDriver

protected GdImage $image;

protected ?string $format = null;

/** @var array<string, mixed> */
protected array $exif = [];

Expand Down Expand Up @@ -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':
Expand All @@ -158,6 +162,7 @@ public function save(?string $path = null): static
if ($this->optimize) {
$this->optimizerChain->optimize($path);
}
$this->format = null;

return $this;
}
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ImageFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit cf72c68

Please sign in to comment.