Skip to content

Commit

Permalink
added a way to load a data uri image
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Dec 27, 2023
1 parent f5ff98c commit b648cbd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Drivers/DecodeDataUriImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace SergiX44\ImageZen\Drivers;

trait DecodeDataUriImage
{

public function isDataUriImage(string $string): bool
{
return str_starts_with($string, 'data:image/');
}

public function decodeDataUriImage(string $string): string
{
if (!$this->isDataUriImage($string)) {
return $string;
}

if (str_contains($string, ';base64,')) {
return base64_decode(explode(';base64,', $string)[1]);
}

return urldecode(explode(',', $string)[1]);
}
}
7 changes: 7 additions & 0 deletions src/Drivers/Gd/Gd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +18,8 @@

class Gd implements Driver
{
use DecodeDataUriImage;

private $data = [];

public function isAvailable(): bool
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Imagick/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,6 +17,8 @@

class Imagick implements Driver
{
use DecodeDataUriImage;

public function isAvailable(): bool
{
return class_exists(class: \Imagick::class) && extension_loaded('imagick');
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b648cbd

Please sign in to comment.