How to Convert a Video to GIF in PHP

Cloudmersive
2 min readJun 28, 2021

--

Converting a video to the social-media favorite GIF format is an excellent way to market a message in an engaging and efficient manner. In this brief tutorial, we will demonstrate how to use an API in PHP to automatically convert a wide variety of video formats to a GIF file. You can even customize the video by inputting optional parameters for max width, max height, aspect ratio, frame rate, start time, and time span.

First, we will run this command to install the SDK:

composer require cloudmersive/cloudmersive_video_api_client

Now, we are ready to call the function by inputting the video file you wish to convert, any of the aforementioned optional parameters you want to specify, and your API key into the following code:

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\VideoApi(


new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$file_url = "file_url_example"; // string | Optional; URL of a video file being used for conversion. Use this option for files larger than 2GB.
$max_width = 56; // int | Optional; Maximum width of the output video, up to the original video width. Defaults to 250 pixels, maximum is 500 pixels.
$max_height = 56; // int | Optional; Maximum height of the output video, up to the original video width. Defaults to 250 pixels, maximum is 500 pixels.
$preserve_aspect_ratio = true; // bool | Optional; If false, the original video's aspect ratio will not be preserved, allowing customization of the aspect ratio using maxWidth and maxHeight, potentially skewing the video. Default is true.
$frame_rate = 56; // int | Optional; Specify the frame rate of the output video. Defaults to 24 frames per second.
$start_time = new \DateTime("2013-10-20T19:20:30+01:00"); // \DateTime | Optional; Specify the desired starting time of the GIF video in TimeSpan format.
$time_span = new \DateTime("2013-10-20T19:20:30+01:00"); // \DateTime | Optional; Specify the desired length of the GIF video in TimeSpan format. Limit is 30 seconds. Default is 10 seconds.
try {
$result = $apiInstance->videoConvertToGif($input_file, $file_url, $max_width, $max_height, $preserve_aspect_ratio, $frame_rate, $start_time, $time_span);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling VideoApi->videoConvertToGif: ', $e->getMessage(), PHP_EOL;
}
?>

Depending on the file size, the process may take a few moments to run, but once it’s complete you will have a downloadable GIF as a result. If you need to obtain an API key, head to the Cloudmersive website to register for a free account; this provides 800 calls/month across any of our APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet