【发布时间】:2015-06-18 10:42:38
【问题描述】:
我正在使用 Imagick 从我的帧数组创建动画 gif。我创建的示例 gif 按预期制作动画,但动画没有循环回到开头。
示例变量:
$durations = array(100, 100);
$loops = 0; // infinite
主代码/create_gif()
$gif = new Imagick();
$gif->setFormat('gif');
for ($i = 0; $i < count($frames); $i++) {
$gif->addImage($frames[$i]);
$gif->setImageDelay($durations[$i]);
}
$gif->setImageIterations($loops);
$gif = $gif->deconstructImages();
$gif->writeImages('test.gif', true);
Here 是我的示例 gif。正如您在加载页面时看到的那样,男人的头发向下摆动(这只是为了测试,在我花时间绘制实际框架之前)并且应该循环并向后摆动,但这从未发生过。
为了循环播放 gif,我可以更改哪些内容?
编辑根据要求,这里是用于构建框架的(稍微删减的)代码:
// Build an array of still frames
$frames = array();
for ($i = 0; $i < $frame_num; $i++) {
// Build an array of image layers
$layers = array();
for ($j = 0; $j < 11; $j++) {
$layers[$j] = new Imagick();
$layers[$j]->readImage('path_to_this_layer.gif');
}
// Combine the image layers into a frame
$frames[$i] = create_frame($layers);
}
// Combine the frames into an animated gif
$gif = create_gif($image_string, $frames, $durations, $loops);
// create_gif() triggers the code I originally posted
create_frame():
$frame = new Imagick();
$frame->readImageBlob($layers[0]);
for ($i = 1; $i < count($layers); $i++) {
if ($layers[$i] != false) {
$frame->compositeImage($layers[$i], Imagick::COMPOSITE_DEFAULT, 0, 0);
}
}
$frame->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
return $frame;
【问题讨论】:
-
$frames 的数量是多少?
-
echo count($frames);返回2 -
你能显示代码吗,你是如何收到 $frames 的?
-
@AshotKhanamiryan 我已将其添加到主帖中。
-
试试 $gif->setImageIterations(0);而是 $loops