first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Gif\Traits;
|
||||
|
||||
use Intervention\Gif\Decoders\AbstractDecoder;
|
||||
use Intervention\Gif\Exceptions\DecoderException;
|
||||
|
||||
trait CanDecode
|
||||
{
|
||||
/**
|
||||
* Decode current instance
|
||||
*
|
||||
* @throws DecoderException
|
||||
*/
|
||||
public static function decode(mixed $source, ?int $length = null): mixed
|
||||
{
|
||||
return self::getDecoder($source, $length)->decode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get decoder for current instance
|
||||
*
|
||||
* @throws DecoderException
|
||||
*/
|
||||
protected static function getDecoder(mixed $source, ?int $length = null): AbstractDecoder
|
||||
{
|
||||
$classname = sprintf('Intervention\Gif\Decoders\%sDecoder', self::getShortClassname());
|
||||
|
||||
if (!class_exists($classname)) {
|
||||
throw new DecoderException("Decoder for '" . static::class . "' not found.");
|
||||
}
|
||||
|
||||
$decoder = new $classname($source, $length);
|
||||
|
||||
if (!($decoder instanceof AbstractDecoder)) {
|
||||
throw new DecoderException("Decoder for '" . static::class . "' not found.");
|
||||
}
|
||||
|
||||
return $decoder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Gif\Traits;
|
||||
|
||||
use Intervention\Gif\Encoders\AbstractEncoder;
|
||||
use Intervention\Gif\Exceptions\EncoderException;
|
||||
|
||||
trait CanEncode
|
||||
{
|
||||
/**
|
||||
* Encode current entity
|
||||
*
|
||||
* @throws EncoderException
|
||||
*/
|
||||
public function encode(): string
|
||||
{
|
||||
return $this->getEncoder()->encode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get encoder object for current entity
|
||||
*
|
||||
* @throws EncoderException
|
||||
*/
|
||||
protected function getEncoder(): AbstractEncoder
|
||||
{
|
||||
$classname = sprintf('Intervention\Gif\Encoders\%sEncoder', $this->getShortClassname());
|
||||
|
||||
if (!class_exists($classname)) {
|
||||
throw new EncoderException("Encoder for '" . $this::class . "' not found.");
|
||||
}
|
||||
|
||||
$encoder = new $classname($this);
|
||||
|
||||
if (!($encoder instanceof AbstractEncoder)) {
|
||||
throw new EncoderException("Encoder for '" . $this::class . "' not found.");
|
||||
}
|
||||
|
||||
return $encoder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Gif\Traits;
|
||||
|
||||
use Intervention\Gif\Exceptions\RuntimeException;
|
||||
|
||||
trait CanHandleFiles
|
||||
{
|
||||
/**
|
||||
* Determines if input is file path
|
||||
*/
|
||||
private static function isFilePath(mixed $input): bool
|
||||
{
|
||||
return is_string($input) && !self::hasNullBytes($input) && @is_file($input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if given string contains null bytes
|
||||
*/
|
||||
private static function hasNullBytes(string $string): bool
|
||||
{
|
||||
return str_contains($string, chr(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create file pointer from given gif image data
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
private static function getHandleFromData(string $data): mixed
|
||||
{
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
|
||||
if ($handle === false) {
|
||||
throw new RuntimeException('Unable to create tempory file handle.');
|
||||
}
|
||||
|
||||
fwrite($handle, $data);
|
||||
rewind($handle);
|
||||
|
||||
return $handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create file pounter from given file path
|
||||
*/
|
||||
private static function getHandleFromFilePath(string $path): mixed
|
||||
{
|
||||
return fopen($path, 'rb');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user