1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-08 14:56:32 +02:00

Refactor code

This commit is contained in:
Oliver Vogel
2023-11-11 10:51:15 +01:00
parent ed8cd91399
commit 6167e8e233

View File

@@ -2,6 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Decoders; namespace Intervention\Image\Drivers\Gd\Decoders;
use GdImage;
use Intervention\Image\Collection; use Intervention\Image\Collection;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder; use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Gd\Frame; use Intervention\Image\Drivers\Gd\Frame;
@@ -30,17 +31,7 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
return $this->decodeGif($input); // decode (animated) gif return $this->decodeGif($input); // decode (animated) gif
} }
$gd = @imagecreatefromstring($input); $gd = $this->coreFromString($input);
if ($gd === false) {
throw new DecoderException('Unable to decode input');
}
if (! imageistruecolor($gd)) {
imagepalettetotruecolor($gd);
}
imagesavealpha($gd, true);
// build image instance // build image instance
$image = new Image(new Collection([new Frame($gd)])); $image = new Image(new Collection([new Frame($gd)]));
@@ -59,19 +50,40 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
}; };
} }
protected function decodeGif($input): ImageInterface private function coreFromString(string $input): GdImage
{ {
$image = new Image(new Collection()); $gd = @imagecreatefromstring($input);
$gif = GifDecoder::decode($input);
if ($gd === false) {
if (!$gif->isAnimated()) { throw new DecoderException('Unable to decode input');
return $image->addFrame(new Frame(@imagecreatefromstring($input)));
} }
if (!imageistruecolor($gd)) {
imagepalettetotruecolor($gd);
}
imagesavealpha($gd, true);
return $gd;
}
private function decodeGif(string $input): ImageInterface
{
$gif = GifDecoder::decode($input);
if (!$gif->isAnimated()) {
return new Image(
new Collection([new Frame(
$this->coreFromString($input)
)]),
);
}
$image = new Image(new Collection());
$image->setLoops($gif->getMainApplicationExtension()?->getLoops()); $image->setLoops($gif->getMainApplicationExtension()?->getLoops());
$splitter = GifSplitter::create($gif)->split(); $splitter = GifSplitter::create($gif)->split();
$delays = $splitter->getDelays(); $delays = $splitter->getDelays();
foreach ($splitter->coalesceToResources() as $key => $gd) { foreach ($splitter->coalesceToResources() as $key => $gd) {
$image->addFrame((new Frame($gd))->setDelay($delays[$key] / 100)); $image->addFrame((new Frame($gd))->setDelay($delays[$key] / 100));