Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分解动画GIF和操纵帧(GD库)_Php_Animated Gif - Fatal编程技术网

Php 分解动画GIF和操纵帧(GD库)

Php 分解动画GIF和操纵帧(GD库),php,animated-gif,Php,Animated 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

因此,由于动画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头添加到每一组图像数据中,但如果可能的话,我强烈建议使用PHP ImageMagick接口——它对迭代图像中的帧有更好的支持

另一种选择是使用纯PHP实现,它可以实现您想要的功能,例如

有关守则如下:


如您所见,要使其成为有效的GIF数据,需要更多的数据(标题、扩展名(87a vs 89)和终止字符)。

在Imagemagick中,这是非常简单的。您可以合并图像以填充任何已优化的帧,然后进行处理,然后再次优化

输入:



谢谢,这是一个很好的信息。我使用的是ImageMagick,但对于我正在尝试创建的比例(动态图像),它的速度太慢了,令人无法忍受。你给了我很多思考,我会考虑一些比GIF更为优化的东西,有很多不错的近乎实时的转换工具来传输MP4等等(GIF需要的带宽将通过屋顶)。
$img = imagecreatefromstring(
    $this->fileHeader["gifheader"] .
    $this->frameSources[$i]["graphicsextension"] .
    $this->frameSources[$i]["imagedata"] .
    chr(0x3b)
);
convert bunny.gif -coalesce -resize 50% -layers optimize bunny_small.gif