There are multiple reasons why one might want to convert a video into a GIF. For instance, you may want to create a short animated GIF by extracting a specific segment from an MP4 file. To do this in your PHP application, you'll need an API that supports GIF output. Here's how it's done in a few lines of PHP code:
// Don't forget to install the required library using Composer:
// composer require creatomate/creatomate
$client = new Creatomate\Client('Your API Key');
$output = $client->render([
'source' => new Creatomate\Source([
'output_format' => 'gif',
'gif_quality' => 'best',
'gif_compression' => 100,
'frame_rate' => '10 fps',
'duration' => 3,
'elements' => [
[
'type' => 'video',
'source' => 'https://cdn.creatomate.com/demo/bird.mp4'
]
]
])
]);
var_dump($output);
How it works: Start by installing the Creatomate library from Packagist using Composer. This library offers a bunch of handy features for working with GIFs. With that set up, you can now generate pretty much any kind of GIF from PHP code.
There are several output formats available. Here, we want to create a GIF, so we set "output_format" to "gif". Other output formats include "jpg", "png", or "mp4". When exporting a GIF, you can play with the "gif_quality" and "gif_compression" options. When it comes to quality or speed, you can choose between "best" or "fast". The "gif_compression" parameter allows the GIF file to be compressed further, with a number between 0 and 200. Keep in mind that the higher the compression level, the more artifacts may appear.
In addition to the "frame_rate" (e.g., 10 frames per second), specify the duration of the GIF (e.g., 3 seconds) and optionally set the "width" and "height" parameters to crop the GIF to a specific resolution.
If you want to add a logo or caption to your GIF, you can do that too. Take a look at the related examples below for inspiration, or try out the video editor. This online tool allows you to create your own GIF overlays and gives you the code to paste into your PHP application.