viicslen / anim-gif
PHP类,用于从多个图片创建动画GIF
1.4
2020-12-04 21:26 UTC
Requires
- php: >=5.3.0
README
注意:这是 Clément Guillemain 的GifCreator 类的分支,进行了一些API更改(类名更改、新方法和更新方法、更灵活(更稳健)的参数处理等),更好的错误处理,几个小的修正,代码美化以及其他改进。
关于
AnimGif 是一个PHP类,用于创建动画GIF -- 只需列出源图片(各种形式),就是这样!
安装
composer install viicslen/AnimGif
使用
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格式剖析页面。