PHP - 从两个 JPEG 图像创建简单的动画 GIF?

标签 php image gif animated-gif

<分区>

有谁知道是否可以从两个不同的 JPEG 文件生成 GIF 动画,一个图像显示 x 秒,然后另一个图像,依此类推......?

感谢任何建议。

谢谢。

最佳答案

有关不错、快速和更新的解决方案,请参阅 this SO answer .

对于更新的解决方案,here is my fork其中,有一些小的修复和改进。一个来自实际应用的例子:

$anim = new GifCreator\AnimGif();

$gif = $anim->create($image_files);
//file_put_contents("test.gif", $gif);

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

(需要 PHP5.3 和 GD2。)

适用于 PHP 5.6 和 GD 2.4.11 的示例:

require_once "AnimGif.php";

/*
 * Create an array containing file paths, resource var (initialized with imagecreatefromXXX), 
 * image URLs or even binary code from image files.
 * All sorted in order to appear.
 */
$image_files = array(
    //imagecreatefrompng("/../images/pic1.png"), // Resource var
    //"/../images/pic2.png", // Image file path
    //file_get_contents("/../images/pic3.jpg"), // Binary source code
    'https://yt3.ggpht.com/-KxeE9Hu93eE/AAAAAAAAAAI/AAAAAAAAAAA/D-DB1Umuimk/s100-c-k-no-mo-rj-c0xffffff/photo.jpg', // URL
    'https://media.licdn.com/mpr/mpr/shrinknp_100_100/AAEAAQAAAAAAAAloAAAAJDRkZGY2MWZmLTM1NDYtNDBhOS04MjYwLWNkM2UzYjdiZGZmMA.png', // URL
    'http://is5.mzstatic.com/image/thumb/Purple128/v4/e4/63/e7/e463e779-e6d0-0c3d-3ec1-97fdbaae230a/source/100x100bb.jpg' // URL
);

/*
 * Create an array containing the duration (in millisecond) of each frame.
 */
$durations_millis = array(
    1000,
    2000,
    3000
);

/*
 * Fix durations.
 */
$durations = array();
for ($i = 0; $i < count($durations_millis); $i++) {
    $durations[$i] = $durations_millis[$i] / 10;
}

/*
 * Specify number of loops. (0 = infinite looping.)
 */
$num_loops = 0;

/*
 * Create gif object.
 */
$anim_gif = new GifCreator\AnimGif();
$gif_object = $anim_gif->create($image_files, $durations, $num_loops);

/*
 * Get the animated GIF binary.
 */
$gif_binary = $gif_object->get();

/*
 * Set the file name of the saved/returned animated GIF.
 */
$file_name = "animated.gif";

/*
 *  Optionally, save animated GIF in a folder as a GIF:
 */
//file_put_contents($file_name, $gif_binary);

/*
 * Optionally, return the animated GIF to client.
 */
header("Content-type: image/gif");
header('Content-Disposition: filename="' . $file_name . '"'); // Optional
echo $gif_binary;

/*
 * All done.
 */
exit;

关于PHP - 从两个 JPEG 图像创建简单的动画 GIF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2191367/

相关文章:

php - 从数据库获取数据并与静态数组匹配

PHP MySQL Ajax 更新记录

php - 依赖MySQL Cascade删除安全吗

Java 从字体中获取 ASCII 字符集并在白色背景上分别绘制每个字符?

c++ - 无法使用 IMG_Load() 加载图像

php - 使用 PHP GD 缩小动画 GIF

PHP 数学方程,E+16?

android - 单击按钮获取并显示图像

java - 在 Android 中显示来自休息服务器的 gif/动画 ImageView

Android,使用ffmpeg将gif分割成帧