Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decoding error with trailing comment extension blocks #12

Merged
merged 3 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Decoders/GifDataStreamDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Intervention\Gif\Decoders;

use Intervention\Gif\AbstractExtension;
use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\Header;
use Intervention\Gif\Blocks\LogicalScreenDescriptor;
Expand Down Expand Up @@ -38,7 +40,16 @@ public function decode(): GifDataStream
}

while ($this->viewNextByte() != Trailer::MARKER) {
$gif->addFrame(FrameBlock::decode($this->handle));
match ($this->viewNextBytes(2)) {
// trailing "global" comment blocks which are not part of "FrameBlock"
AbstractExtension::MARKER . CommentExtension::LABEL
=> $gif->addComment(
CommentExtension::decode($this->handle)
),
default => $gif->addFrame(
FrameBlock::decode($this->handle)
),
};
}

return $gif;
Expand Down
13 changes: 13 additions & 0 deletions src/Encoders/GifDataStreamEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function encode(): string
$this->source->getLogicalScreenDescriptor()->encode(),
$this->maybeEncodeGlobalColorTable(),
$this->encodeFrames(),
$this->encodeComments(),
$this->source->getTrailer()->encode(),
]);
}
Expand All @@ -54,4 +55,16 @@ protected function encodeFrames(): string
return $frame->encode();
}, $this->source->getFrames()));
}

/**
* Encode comment extension blocks of source
*
* @return string
*/
protected function encodeComments(): string
{
return implode('', array_map(function ($commentExtension) {
return $commentExtension->encode();
}, $this->source->getComments()));
}
}
27 changes: 26 additions & 1 deletion src/GifDataStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Intervention\Gif;

use Intervention\Gif\Blocks\ColorTable;
use Intervention\Gif\Blocks\CommentExtension;
use Intervention\Gif\Blocks\FrameBlock;
use Intervention\Gif\Blocks\Header;
use Intervention\Gif\Blocks\LogicalScreenDescriptor;
Expand All @@ -20,7 +21,8 @@ public function __construct(
protected Header $header = new Header(),
protected LogicalScreenDescriptor $logicalScreenDescriptor = new LogicalScreenDescriptor(),
protected ?ColorTable $globalColorTable = null,
protected array $frames = []
protected array $frames = [],
protected array $comments = []
) {
}

Expand Down Expand Up @@ -122,6 +124,16 @@ public function getFrames(): array
return $this->frames;
}

/**
* Return array of "global" comments
*
* @return array
*/
public function getComments(): array
{
return $this->comments;
}

/**
* Return first frame
*
Expand Down Expand Up @@ -149,6 +161,19 @@ public function addFrame(FrameBlock $frame): self
return $this;
}

/**
* Add comment extension
*
* @param CommentExtension $comment
* @return GifDataStream
*/
public function addComment(CommentExtension $comment): self
{
$this->comments[] = $comment;

return $this;
}

/**
* Set the current data
*
Expand Down
14 changes: 14 additions & 0 deletions tests/GifDataStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testEncode()
$gif = new GifDataStream();
$gif->setLogicalScreenDescriptor($this->getTestLogicalScreenDescriptor());
$gif->addFrame($this->getTestFrame());
$gif->addComment($this->getTestCommentExtension());

$result = implode('', [
(string) $this->getTestHeader(),
Expand All @@ -42,6 +43,7 @@ public function testEncode()
(string) $this->getTestGraphicControlExtension(),
(string) $this->getTestImageDescriptor(),
(string) $this->getTestImageData(),
(string) $this->getTestCommentExtension(),
Trailer::MARKER,
]);

Expand Down Expand Up @@ -153,4 +155,16 @@ public function testDecode()
}, $gif->getFrames()));
$this->assertEquals([0, 0, 0, 0, 0, 0, 0, 0], $sizes);
}

public function testDecodeTrailingComment(): void
{
$gif = GifDataStream::decode(
$this->getTestHandle(
file_get_contents(__DIR__ . '/images/animation_trailing_comment.gif')
),
);

$this->assertInstanceOf(GifDataStream::class, $gif);
$this->assertCount(1, $gif->getComments());
}
}
Binary file added tests/images/animation_trailing_comment.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading