Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Sep 21, 2024
1 parent 93ca996 commit a3b5359
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
33 changes: 19 additions & 14 deletions src/Drivers/Gd/Decoders/NativeObjectDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Drivers\Gd\Decoders;

use Exception;
use GdImage;
use Intervention\Gif\Decoder as GifDecoder;
use Intervention\Gif\Splitter as GifSplitter;
Expand Down Expand Up @@ -76,23 +77,27 @@ protected function decodeGif(mixed $input): ImageInterface
return $image;
}

// create empty core
$core = new Core();
try {
// create empty core
$core = new Core();

$gif = GifDecoder::decode($input);
$splitter = GifSplitter::create($gif)->split();
$delays = $splitter->getDelays();
$gif = GifDecoder::decode($input);
$splitter = GifSplitter::create($gif)->split();
$delays = $splitter->getDelays();

// set loops on core
if ($loops = $gif->getMainApplicationExtension()?->getLoops()) {
$core->setLoops($loops);
}
// set loops on core
if ($loops = $gif->getMainApplicationExtension()?->getLoops()) {
$core->setLoops($loops);
}

// add GDImage instances to core
foreach ($splitter->coalesceToResources() as $key => $native) {
$core->push(
new Frame($native, $delays[$key] / 100)
);
// add GDImage instances to core
foreach ($splitter->coalesceToResources() as $key => $native) {
$core->push(
new Frame($native, $delays[$key] / 100)
);
}
} catch (Exception $e) {
throw new DecoderException($e->getMessage(), $e->getCode(), $e);
}

// create (possibly) animated image
Expand Down
28 changes: 14 additions & 14 deletions src/Drivers/Gd/Encoders/GifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,25 @@ public function encode(ImageInterface $image): EncodedImage
*/
protected function encodeAnimated(ImageInterface $image): EncodedImage
{
$builder = GifBuilder::canvas(
$image->width(),
$image->height()
);

foreach ($image as $frame) {
$builder->addFrame(
source: $this->encode($frame->toImage($image->driver()))->toFilePointer(),
delay: $frame->delay(),
interlaced: $this->interlaced
try {
$builder = GifBuilder::canvas(
$image->width(),
$image->height()
);
}

try {
foreach ($image as $frame) {
$builder->addFrame(
source: $this->encode($frame->toImage($image->driver()))->toFilePointer(),
delay: $frame->delay(),
interlaced: $this->interlaced
);
}

$builder->setLoops($image->loops());

return new EncodedImage($builder->encode());
} catch (Exception $e) {
throw new EncoderException($e->getMessage(), $e->getCode(), $e);
}

return new EncodedImage($builder->encode());
}
}

0 comments on commit a3b5359

Please sign in to comment.