分解动画 GIF 和操纵帧(GD 库)
Exploding Animated GIF and Manipulating Frames (GD Library)
因此,由于动画 GIF 是一系列与“\x00\x21\xF9\x04”连接在一起的 GIF,我能够分解 GIF 并将其内爆以将其拆开并重新构建。但是我似乎无法让 GD 从数据中创建图像。
为了让 GD 识别数据,我需要附加什么吗?
$im = file_get_contents('test.gif'); //get the data for the file
$imgarray = explode("\x00\x21\xF9\x04", $im); //split up the frames
foreach ($imgarray as $frame) {
$img[] = imagecreatefromstring($frame); //this is the line that fails
}
$new_gif = implode("\x00\x21\xF9\x04", $img); //this should work but imagecreatefromstring fails
$new_gif = implode("\x00\x21\xF9\x04", $imgarray); (Does work as it just puts the image back together)
GIF 不仅仅包含相互附加的单独图像。 GIF 中的帧可能只改变图像的一部分 - 它不必覆盖整个帧。它还可以包含一个局部调色板,但除此之外它依赖于图像的全局调色板——它是为文件本身而不仅仅是帧存储的。
即- 你不能只是分解文件并分别解码每个片段,除了从 GD 获得有用的图像。
您至少需要将 gif header 添加到每组图像数据中,但我强烈建议尽可能使用 PHP ImageMagick 界面 - 它对遍历图像中的帧。
另一种选择是使用纯 PHP 实现来满足您的需求,例如 GifFrameExtractor.
相关代码为located at line 137 of the source file:
$img = imagecreatefromstring(
$this->fileHeader["gifheader"] .
$this->frameSources[$i]["graphicsextension"] .
$this->frameSources[$i]["imagedata"] .
chr(0x3b)
);
如您所见,需要更多数据(header、扩展名(87a 对 89)和终止符)才能使其成为有效的 GIF 数据。
在 Imagemagick 中,这非常简单。您可以合并图像以填充任何已优化的帧,然后进行处理,然后再次优化。
输入:
convert bunny.gif -coalesce -resize 50% -layers optimize bunny_small.gif
因此,由于动画 GIF 是一系列与“\x00\x21\xF9\x04”连接在一起的 GIF,我能够分解 GIF 并将其内爆以将其拆开并重新构建。但是我似乎无法让 GD 从数据中创建图像。
为了让 GD 识别数据,我需要附加什么吗?
$im = file_get_contents('test.gif'); //get the data for the file
$imgarray = explode("\x00\x21\xF9\x04", $im); //split up the frames
foreach ($imgarray as $frame) {
$img[] = imagecreatefromstring($frame); //this is the line that fails
}
$new_gif = implode("\x00\x21\xF9\x04", $img); //this should work but imagecreatefromstring fails
$new_gif = implode("\x00\x21\xF9\x04", $imgarray); (Does work as it just puts the image back together)
GIF 不仅仅包含相互附加的单独图像。 GIF 中的帧可能只改变图像的一部分 - 它不必覆盖整个帧。它还可以包含一个局部调色板,但除此之外它依赖于图像的全局调色板——它是为文件本身而不仅仅是帧存储的。
即- 你不能只是分解文件并分别解码每个片段,除了从 GD 获得有用的图像。
您至少需要将 gif header 添加到每组图像数据中,但我强烈建议尽可能使用 PHP ImageMagick 界面 - 它对遍历图像中的帧。
另一种选择是使用纯 PHP 实现来满足您的需求,例如 GifFrameExtractor.
相关代码为located at line 137 of the source file:
$img = imagecreatefromstring(
$this->fileHeader["gifheader"] .
$this->frameSources[$i]["graphicsextension"] .
$this->frameSources[$i]["imagedata"] .
chr(0x3b)
);
如您所见,需要更多数据(header、扩展名(87a 对 89)和终止符)才能使其成为有效的 GIF 数据。
在 Imagemagick 中,这非常简单。您可以合并图像以填充任何已优化的帧,然后进行处理,然后再次优化。
输入:
convert bunny.gif -coalesce -resize 50% -layers optimize bunny_small.gif