diff --git a/src/Drivers/Abstract/Decoders/AbstractDecoder.php b/src/Drivers/Abstract/Decoders/AbstractDecoder.php index 70ad5433..76f4ba95 100644 --- a/src/Drivers/Abstract/Decoders/AbstractDecoder.php +++ b/src/Drivers/Abstract/Decoders/AbstractDecoder.php @@ -69,4 +69,76 @@ abstract class AbstractDecoder implements DecoderInterface return base64_encode(base64_decode($input)) === str_replace(["\n", "\r"], '', $input); } + + /** + * Parse data uri + * + * @param mixed $value + * @return object + */ + protected function parseDataUri($value): object + { + $pattern = "/^data:(?P\w+\/[-+.\w]+)?" . + "(?P(;[-\w]+=[-\w]+)*)(?P;base64)?,(?P.*)/"; + + $result = preg_match($pattern, $value, $matches); + + return new class ($matches, $result) + { + private $matches; + private $result; + + public function __construct($matches, $result) + { + $this->matches = $matches; + $this->result = $result; + } + + public function isValid(): bool + { + return (bool) $this->result; + } + + public function mediaType(): ?string + { + if (isset($this->matches['mediatype']) && !empty($this->matches['mediatype'])) { + return $this->matches['mediatype']; + } + + return null; + } + + public function hasMediaType(): bool + { + return !empty($this->mediaType()); + } + + public function parameters(): array + { + if (isset($this->matches['parameters']) && !empty($this->matches['parameters'])) { + return explode(';', trim($this->matches['parameters'], ';')); + } + + return []; + } + + public function isBase64Encoded(): bool + { + if (isset($this->matches['base64']) && $this->matches['base64'] === ';base64') { + return true; + } + + return false; + } + + public function data(): ?string + { + if (isset($this->matches['data']) && !empty($this->matches['data'])) { + return $this->matches['data']; + } + + return null; + } + }; + } } diff --git a/src/Drivers/Gd/Decoders/DataUriImageDecoder.php b/src/Drivers/Gd/Decoders/DataUriImageDecoder.php index 32155781..5d511092 100644 --- a/src/Drivers/Gd/Decoders/DataUriImageDecoder.php +++ b/src/Drivers/Gd/Decoders/DataUriImageDecoder.php @@ -6,19 +6,16 @@ use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Traits\CanDecodeDataUri; class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface { - use CanDecodeDataUri; - public function decode($input): ImageInterface|ColorInterface { if (!is_string($input)) { throw new DecoderException('Unable to decode input'); } - $uri = $this->decodeDataUri($input); + $uri = $this->parseDataUri($input); if (! $uri->isValid()) { throw new DecoderException('Unable to decode input'); diff --git a/src/Drivers/Imagick/Decoders/DataUriImageDecoder.php b/src/Drivers/Imagick/Decoders/DataUriImageDecoder.php index 1dcff224..5bce2903 100644 --- a/src/Drivers/Imagick/Decoders/DataUriImageDecoder.php +++ b/src/Drivers/Imagick/Decoders/DataUriImageDecoder.php @@ -6,19 +6,16 @@ use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DecoderInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Traits\CanDecodeDataUri; class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface { - use CanDecodeDataUri; - public function decode($input): ImageInterface|ColorInterface { if (!is_string($input)) { throw new DecoderException('Unable to decode input'); } - $uri = $this->decodeDataUri($input); + $uri = $this->parseDataUri($input); if (! $uri->isValid()) { throw new DecoderException('Unable to decode input'); diff --git a/src/Traits/CanDecodeDataUri.php b/src/Traits/CanDecodeDataUri.php deleted file mode 100644 index b19f06cd..00000000 --- a/src/Traits/CanDecodeDataUri.php +++ /dev/null @@ -1,77 +0,0 @@ -\w+\/[-+.\w]+)?" . - "(?P(;[-\w]+=[-\w]+)*)(?P;base64)?,(?P.*)/"; - - $result = preg_match($pattern, $value, $matches); - - return new class ($matches, $result) - { - private $matches; - private $result; - - public function __construct($matches, $result) - { - $this->matches = $matches; - $this->result = $result; - } - - public function isValid(): bool - { - return (bool) $this->result; - } - - public function mediaType(): ?string - { - if (isset($this->matches['mediatype']) && !empty($this->matches['mediatype'])) { - return $this->matches['mediatype']; - } - - return null; - } - - public function hasMediaType(): bool - { - return !empty($this->mediaType()); - } - - public function parameters(): array - { - if (isset($this->matches['parameters']) && !empty($this->matches['parameters'])) { - return explode(';', trim($this->matches['parameters'], ';')); - } - - return []; - } - - public function isBase64Encoded(): bool - { - if (isset($this->matches['base64']) && $this->matches['base64'] === ';base64') { - return true; - } - - return false; - } - - public function data(): ?string - { - if (isset($this->matches['data']) && !empty($this->matches['data'])) { - return $this->matches['data']; - } - - return null; - } - }; - } -}