mirror of
https://github.com/Intervention/image.git
synced 2025-08-20 04:31:24 +02:00
Move method from trait to AbstractDecoder
This commit is contained in:
@@ -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<mediatype>\w+\/[-+.\w]+)?" .
|
||||
"(?P<parameters>(;[-\w]+=[-\w]+)*)(?P<base64>;base64)?,(?P<data>.*)/";
|
||||
|
||||
$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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@@ -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');
|
||||
|
@@ -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');
|
||||
|
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Traits;
|
||||
|
||||
trait CanDecodeDataUri
|
||||
{
|
||||
/**
|
||||
* Parse data url info from given value
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function decodeDataUri($value): object
|
||||
{
|
||||
$pattern = "/^data:(?P<mediatype>\w+\/[-+.\w]+)?" .
|
||||
"(?P<parameters>(;[-\w]+=[-\w]+)*)(?P<base64>;base64)?,(?P<data>.*)/";
|
||||
|
||||
$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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user