-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor image decoding process of GD driver
- Loading branch information
1 parent
ef8af61
commit 03a5928
Showing
7 changed files
with
218 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Image\Drivers\Gd\Decoders; | ||
|
||
use GdImage; | ||
use Intervention\Image\Drivers\AbstractDecoder; | ||
use Intervention\Image\Drivers\Gd\Core; | ||
use Intervention\Image\Drivers\Gd\Driver; | ||
use Intervention\Image\Drivers\Gd\Frame; | ||
use Intervention\Image\Exceptions\DecoderException; | ||
use Intervention\Image\Image; | ||
use Intervention\Image\Interfaces\ImageInterface; | ||
use Intervention\Image\Interfaces\ColorInterface; | ||
|
||
class GdImageDecoder extends AbstractDecoder | ||
{ | ||
public function decode(mixed $input): ImageInterface|ColorInterface | ||
{ | ||
if (!is_object($input)) { | ||
throw new DecoderException('Unable to decode input'); | ||
} | ||
|
||
if (!($input instanceof GdImage)) { | ||
throw new DecoderException('Unable to decode input'); | ||
} | ||
|
||
if (!imageistruecolor($input)) { | ||
imagepalettetotruecolor($input); | ||
} | ||
|
||
imagesavealpha($input, true); | ||
|
||
// build image instance | ||
return new Image( | ||
new Driver(), | ||
new Core([ | ||
new Frame($input) | ||
]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Intervention\Image\Drivers\Gd\Decoders\Traits; | ||
|
||
use Intervention\Gif\Decoder as GifDecoder; | ||
use Intervention\Gif\Splitter as GifSplitter; | ||
use Intervention\Image\Drivers\Gd\Core; | ||
use Intervention\Image\Drivers\Gd\Driver; | ||
use Intervention\Image\Drivers\Gd\Frame; | ||
use Intervention\Image\Image; | ||
use Intervention\Image\Interfaces\ImageInterface; | ||
|
||
trait CanDecodeGif | ||
{ | ||
/** | ||
* Decode image from given GIF source which can be either a file path or binary data | ||
* | ||
* @param mixed $input | ||
* @return ImageInterface | ||
*/ | ||
protected function decodeGif(mixed $input): ImageInterface | ||
{ | ||
$gif = GifDecoder::decode($input); | ||
$splitter = GifSplitter::create($gif)->split(); | ||
$delays = $splitter->getDelays(); | ||
|
||
// build core | ||
$core = new Core(); | ||
|
||
// 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))->setDelay($delays[$key] / 100) | ||
); | ||
} | ||
|
||
return new Image(new Driver(), $core); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters