From b648cbdf39f77299fea1182f22b135c75b198d96 Mon Sep 17 00:00:00 2001 From: Sergio Brighenti Date: Wed, 27 Dec 2023 23:57:51 +0100 Subject: [PATCH] added a way to load a data uri image --- src/Drivers/DecodeDataUriImage.php | 25 +++++++++++++++++++++++++ src/Drivers/Gd/Gd.php | 7 +++++++ src/Drivers/Imagick/Imagick.php | 6 ++++++ tests/DriverTest.php | 9 +++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/Drivers/DecodeDataUriImage.php diff --git a/src/Drivers/DecodeDataUriImage.php b/src/Drivers/DecodeDataUriImage.php new file mode 100644 index 0000000..7623cf1 --- /dev/null +++ b/src/Drivers/DecodeDataUriImage.php @@ -0,0 +1,25 @@ +isDataUriImage($string)) { + return $string; + } + + if (str_contains($string, ';base64,')) { + return base64_decode(explode(';base64,', $string)[1]); + } + + return urldecode(explode(',', $string)[1]); + } +} diff --git a/src/Drivers/Gd/Gd.php b/src/Drivers/Gd/Gd.php index 69f793d..cc1dc07 100644 --- a/src/Drivers/Gd/Gd.php +++ b/src/Drivers/Gd/Gd.php @@ -7,6 +7,7 @@ use Psr\Http\Message\StreamInterface; use SergiX44\ImageZen\Alteration; use SergiX44\ImageZen\Draws\Color; +use SergiX44\ImageZen\Drivers\DecodeDataUriImage; use SergiX44\ImageZen\Drivers\Driver; use SergiX44\ImageZen\Exceptions\AlterationNotImplementedException; use SergiX44\ImageZen\Exceptions\CannotLoadImageException; @@ -17,6 +18,8 @@ class Gd implements Driver { + use DecodeDataUriImage; + private $data = []; public function isAvailable(): bool @@ -62,6 +65,10 @@ public function loadImageFrom(string $path): GdImage $this->data = getimagesizefromstring($data); $resource = imagecreatefromstring($data); } else { + if ($this->isDataUriImage($path)) { + $path = $this->decodeDataUriImage($path); + } + $this->data = getimagesizefromstring($path); $resource = imagecreatefromstring($path); } diff --git a/src/Drivers/Imagick/Imagick.php b/src/Drivers/Imagick/Imagick.php index 3fdee2e..fb3a5c4 100644 --- a/src/Drivers/Imagick/Imagick.php +++ b/src/Drivers/Imagick/Imagick.php @@ -7,6 +7,7 @@ use Psr\Http\Message\StreamInterface; use SergiX44\ImageZen\Alteration; use SergiX44\ImageZen\Draws\Color; +use SergiX44\ImageZen\Drivers\DecodeDataUriImage; use SergiX44\ImageZen\Drivers\Driver; use SergiX44\ImageZen\Exceptions\AlterationNotImplementedException; use SergiX44\ImageZen\Exceptions\CannotLoadImageException; @@ -16,6 +17,8 @@ class Imagick implements Driver { + use DecodeDataUriImage; + public function isAvailable(): bool { return class_exists(class: \Imagick::class) && extension_loaded('imagick'); @@ -46,6 +49,9 @@ public function loadImageFrom(string $path): ImagickBackend $imagick->readImage($path); $imagick->setImageType(defined('\Imagick::IMGTYPE_TRUECOLORALPHA') ? \Imagick::IMGTYPE_TRUECOLORALPHA : \Imagick::IMGTYPE_TRUECOLORMATTE); } else { + if ($this->isDataUriImage($path)) { + $path = $this->decodeDataUriImage($path); + } $imagick->readImageBlob($path); } } catch (\ImagickException $e) { diff --git a/tests/DriverTest.php b/tests/DriverTest.php index 81a78b2..18a719f 100644 --- a/tests/DriverTest.php +++ b/tests/DriverTest.php @@ -633,6 +633,15 @@ function prepare($instance, string $name, Backend $driver, string $ext = 'png'): expect($b64)->toStartWith('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ'); })->with('drivers', 'tile'); +it('it can load a data uri string', function ($driver, $file) { + prepare($this, '_', $driver); + $b64 = Image::make($file, $driver)->base64(\SergiX44\ImageZen\Format::JPG); + expect($b64)->toStartWith('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ'); + + $image = Image::make($b64, $driver); + expect($image->width())->toBe(16)->and($image->height())->toBe(16); +})->with('drivers', 'tile'); + it('can draw a text with a background', function ($driver, $file) { [$out, $expected] = prepare($this, 'fruit_with_text_background', $driver);