lunakid/anim-gif

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

1.3 2014-05-23 23:38 UTC

This package is not auto-updated.

Last update: 2024-09-22 12:09:55 UTC


README

注意:这是一个Clément GuillemainGifCreator类的分支,进行了一些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格式解析页面