maalls/anim-gif

PHP 类,用于从多张图片创建动画 GIF

v1.4.1 2023-05-13 08:10 UTC

This package is not auto-updated.

Last update: 2024-09-29 12:27:39 UTC


README

注意:这是一个 Clément Guillemain 的优秀 GifCreator 类 的分支,进行了一些 API 更改(类名更改、新增和更新方法、更灵活(且健壮)的参数处理等),更好的错误处理,几个小的修正,代码美化以及其它改进。

关于

AnimGif 是一个 PHP 类,用于创建动画 GIF —— 只需列出源图片(各种形式),就是这样!

用法

1. 输入

// Use an array containing file paths, resource vars (initialized with imagecreatefromXXX), 
// image URLs or binary image data.
$frames = array(
    imagecreatefrompng("/../images/pic1.png"),      // resource var
    "/../images/pic2.png",                          // image file path
    file_get_contents("/../images/pic3.jpg"),       // image binary data
    "http://thisisafakedomain.com/images/pic4.jpg", // URL
);

// Or: load images from a dir (sorted, skipping .files):
//$frames = "../images";

// Optionally: set different durations (in 1/100s units) for each frame
$durations = array(20, 30, 10, 10);

// Or: you can leave off repeated values from the end:
//$durations = array(20, 30, 10); // use 10 for the rest
// Or: use 'null' anywhere to re-apply the previous delay:
//$durations = array(250, null, null, 500);

2. 创建 GIF

$anim = new GifCreator\AnimGif();
$anim->create($frames, $durations);

// Or: using the default 100ms even delay:
//$anim->create($frames);

// Or: loop 5 times, then stop:
//$anim->create($frames, $durations, 5); // default: infinite looping

3. 获取/使用结果

现在你可以获取动画 GIF 二进制文件

$gif = $anim->get();

...例如,可以直接发送到浏览器

header("Content-type: image/gif");
echo $gif;
exit;

或者只是保存到文件

$anim->save("animated.gif");

行为

  • 透明度基于第一帧。[!!未经验证:“只有当你给出具有相同透明背景的多帧时,它才会被保存”]
  • 生成的 GIF 的尺寸也基于第一帧。如果你需要调整图片大小以获得相同的尺寸,可以使用这个类:https://github.com/Sybio/ImageWorkshop

依赖

  • PHP 5.3(用于命名空间支持等;现在没有人还在无耻地使用 PHP < 5.3,对吧?!)
  • GD(《imagecreatefromstring》,imagegif,《imagecolortransparent》)

致谢

  • László Zsidi:所有的困难部分都来自他的 GIFEncoder.class.php(也在这里找到了,在 Gist 中这里)。谢谢,Laci!
  • Clément Guillemain:非常实用的、重新设计(“分类”)的 API、扩展和优秀的文档!
  • Matthew Flickinger:他的惊人、无与伦比的 GIF 格式分析页面