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

update GD driver #221

Merged
merged 7 commits into from
Dec 22, 2023
Merged
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
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