Creating GIF animations - Considering migration from PHP Imagick #225
-
I'm currently using PHP Imagick for creating GIF animations. Technically it works well, but the performance is poor, which makes it critical since I constantly need to build fresh animations in real-time. Libvips seem to do a much faster job! After reading thought the PHP Vips docs I couldn't find any information or examples regarding how to build GIF animations. Is it possible to do it all the way using PHP Libvips only (without depending on apps such as Imagick or Gifsicle)? Also one thing confuses me here. How can I set the frame rate and number of loops when generating a GIF animation with PHP Libvips? In the Libvips (not php-libvips) docs i read this: "Use the metadata items loop and delay to set the number of loops for the animation and the frame delays." I would also greatly appreciate a basic example to get started on building GIF animations with PHP Libvips. Many thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hi @lemontango. There's a pyvips example here, if that helps: https://github.com/libvips/pyvips/blob/master/examples/annotate-animation.py Load an animation, add some text to each frame, save again. |
Beta Was this translation helpful? Give feedback.
-
I made a tiny PHP example: #!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Jcupitt\Vips;
#Vips\Config::setLogger(new Vips\DebugLogger());
if (count($argv) != 4) {
echo("usage: ./animate-image.php input-image output-image 'text string'\n");
exit(1);
}
$image = Vips\Image::newFromFile($argv[1]);
$text = Vips\Image::text($argv[3], ["dpi" => 300, "rgba" => true]);
$animation = NULL;
$delay = [];
for ($x = 0; $x < $image->width + $text->width; $x += 10)
{
// append the frame to the image vertically ... we make a very tall, thin
// strip of frames to save
$frame = $image->composite2($text, "over", [
"x" => $x - $text->width,
"y" => $image->height / 2 - $text->height / 2
]);
if ($animation == NULL)
$animation = $frame;
else
$animation = $animation->join($frame, "vertical");
// frame delay in ms
array_push($delay, 30);
}
// set animation properties
$animation->set("delay", $delay);
$animation->set("loop", 0);
$animation->set("page-height", $image->height);
$animation->writeToFile($argv[2]); Run with eg.:
To make: |
Beta Was this translation helpful? Give feedback.
-
Fantastic! Thank you. Exactly what I was looking for. |
Beta Was this translation helpful? Give feedback.
I made a tiny PHP example: