Skip to content

Commit

Permalink
Add ability to decode directly from file pointer (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel authored Sep 20, 2024
1 parent 0f8a118 commit adcdfb0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function setLoops(int $loops): self
* Create new animation frame from given source
* which can be path to a file or GIF image data
*
* @param string $source
* @param string|resource $source
* @param float $delay time delay in seconds
* @param int $left position offset in pixels from left
* @param int $top position offset in pixels from top
Expand All @@ -103,7 +103,7 @@ public function setLoops(int $loops): self
* @return Builder
*/
public function addFrame(
string $source,
mixed $source,
float $delay = 0,
int $left = 0,
int $top = 0,
Expand Down
32 changes: 24 additions & 8 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,34 @@ class Decoder
/**
* Decode given input
*
* @param mixed $input
* @param string|resource $input
* @throws DecoderException
* @return GifDataStream
*/
public static function decode(mixed $input): GifDataStream
{
return GifDataStream::decode(
match (true) {
self::isFilePath($input) => self::getHandleFromFilePath($input),
is_string($input) => self::getHandleFromData($input),
default => throw new DecoderException('Decoder input must be either file path or binary data.')
}
);
$handle = match (true) {
self::isFilePath($input) => self::getHandleFromFilePath($input),
is_string($input) => self::getHandleFromData($input),
self::isFileHandle($input) => $input,
default => throw new DecoderException(
'Decoder input must be either file path, file pointer resource or binary data.'
)
};

rewind($handle);

return GifDataStream::decode($handle);
}

/**
* Determine if input is file pointer resource
*
* @param mixed $input
* @return bool
*/
private static function isFileHandle(mixed $input): bool
{
return is_resource($input) && get_resource_type($input) === 'stream';
}
}
9 changes: 9 additions & 0 deletions tests/Unit/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ public function testAddFrameInterlace(): void
$this->assertEquals(2, $gif->getFirstFrame()->getImageDescriptor()->getTop());
$this->assertTrue($gif->getFirstFrame()->getImageDescriptor()->isInterlaced());
}

public function testAddFrameFromResource(): void
{
$pointer = fopen('php://temp', 'r+');
fwrite($pointer, file_get_contents($this->getTestImagePath('animation1.gif')));
$builder = Builder::canvas(320, 240);
$result = $builder->addFrame($pointer);
$this->assertInstanceOf(Builder::class, $result);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/DecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public function testDecodeFromData(): void
$decoded = Decoder::decode(file_get_contents($this->getTestImagePath('animation1.gif')));
$this->assertInstanceOf(GifDataStream::class, $decoded);
}

public function testDecodeFromFilePointer(): void
{
$pointer = fopen('php://temp', 'r+');
fwrite($pointer, file_get_contents($this->getTestImagePath('animation1.gif')));
$decoded = Decoder::decode($pointer);
$this->assertInstanceOf(GifDataStream::class, $decoded);
}
}

0 comments on commit adcdfb0

Please sign in to comment.