PHP Classes
PHP Classes
elePHPant
Icontem

PHP Tutorial to Create Animated GIF from Online Video - PHP Convert Video to GIF package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP Convert Video to GIF PHP Convert Video to GIF   Blog PHP Convert Video to GIF package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Tutorial to Creat...  
  Post a comment Post a comment   See comments See comments (11)   Trackbacks (0)  

Author:

Posted on:

Package: PHP Convert Video to GIF

Many users like to publish their videos online in many sites but some sites only allow them to publish pictures.

In that case an alternative solution to publishing videos is to convert them to the animated GIF format.

Read this article to learn how to convert your videos available online, in sites like YouTube, into animated GIF using a PHP and the GIFLayer Web service.




Introduction

Capturing the content

Working with our captured image

Some things to consider

Conclusion


giflayer

Introduction

We started out sharing our lives to the world with a blog, our very own personal log. Ever since video portals, like YouTube, made it so simple for us, every day people to make videos available online. The blog was transformed into a vlog, our very own personal video log.

We could now share our lives in all its full motion glory, and share we did. In some cases we have over-shared, however that is a topic for an entirely different article.

The blog has not gone away entirely, we still use it to let the world know where they can find our vlog. We may even embed part of the video in our post to drive more traffic to our video, after all it is all about the views, right? We also make it available for syndication, using a frame of the video as the image in the feed, cross our fingers and hope it goes viral.

OK, this is not an article on how to produce viral videos, it is supposed to be about converting online video into animated GIF's. I will be using the vlog scenario to demonstrate how you can provide a video clip without using iframe embeds, or how to replace that static image in an RSS feed with an animated one.

We will be using the PHP Convert Video to GIF package which uses a web service API provided by www.giflayer.com.

Capturing the content

The giflayer API supports converting video hosted on most of the popular video portals, for this demonstration I will be using YouTube. The video I will be using is released under a royalty free creative commons license so that there will be no legal issues.

It is important to remember that you are limited on how you can use someone else's creative work. If you are going to be capturing video that is not your own, my best advice is to get the owners permission to use it.

So, we have downloaded the package and set it up with our own personal API key obtained through giflayer. The first thing we will do is instantiate the class and get it ready for us to use.

<?php

set_time_limit(0);
include( 'giflayer.class.php' );
$gifLayer = new gifLayer();

?>

The video we want to capture is located at 'https://www.youtube.com/watch?v=juOPEF_XdP0', you would change this to your actual video location. We want to start our capture 31 seconds into the video and end it at the 41 second mark. To do this, we set our parameters by adding the following code after our instantiation.

$gifLayer->setParam( 'url', 'https://www.youtube.com/' . 'watch?v=juOPEF_XdP0');
$gifLayer->setParam( 'start', 31);
$gifLayer->setParam( 'end', 41);

These are our required parameters. By default our returned image would be 300 pixels wide and 200 pixel high, we only want it half that size, so we set an optional parameter.

$gifLayer->setParam( 'size', '150x100' );

The setParam method is used to pass any valid API parameter and its value to the API. These parameter can be found in the manual.txt file that came with the package.

We are now ready to send our conversion request to the giflayer API and capture the returned binary data for the image.

$gifLayer->captureGif();

Working with our captured image

If everything was successful, the binary data for our image is now stored in 'capture' class property for us to process.

We could save it to a database as a blob (binary large object), we could download it through our browser using the downloadCapture method or we could display it directly to our browser using the displayCapture method.

Since we are going to include this in our blog, about our vlog, we are going to save it as a file on our server.

file_put_contents( 'waterdrop.gif', $gifLayer->capture);

The whole processing file would look something like this...

<?php

set_time_limit(0);
include( 'giflayer.class.php' );
$gifLayer = new gifLayer();
$gifLayer->setParam( 'url', 'https://www.youtube.com/' . 'watch?v=juOPEF_XdP0');
$gifLayer->setParam( 'start', 31);
$gifLayer->setParam( 'end', 41);
$gifLayer->setParam( 'size', '150x100');
$gifLayer->captureGif();
$result = file_put_contents( 'waterdrop.gif', $gifLayer->capture);
if( $result === false ) {
  echo 'problem saving the file';
} else {
  echo 'file has been saved';
}

?>

That is it, we now have our animated image stored on our server where we can include it in our blog and provide the link to it in the RSS feed.

Some things to consider

I already talked about copyright infringement earlier, however it is important enough to mention it one more time. If you are going to use someone else's creative works, make sure you comply with your local copyright laws. The best way to do this is to get the owners permission to use it.

The package comes with 2 methods to automatically display the animated image, they are the displayImage and displayCapture methods. Both of these methods make a call to the API to request the image binary.

If you are going to want to display the image more than once you should save the image capture. You can do this using the downloadCapture method, save the image to a database as a blob or directly to the file system as we did in the example.

The free plan from giflayer allows you to generate 25 images a month and limits them to 10 second clips at a maximum of 10 frames per second. If you are going to require longer clips with better resolution, I encourage you to check out their commercial plans.

If the video is protected from remote viewing by the video portal, you will receive a 210 invalid_url error. This can be confusing since the url you provided is correct, however it is reported as invalid since the portal will not deliver the content.

Conclusion

I have used the vlog scenario to demonstrate the power behind the giflayer API as one possible usage. You may need to convert online video to animated GIF's for any number of other reasons. Whatever those reasons may be, the PHP Convert Video to GIF package will greatly simplify the conversion task.

If you liked this article or have a question about converting videos to animated GIF images, post a comment here.



You need to be a registered user or login to post a comment

1,447,365 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

4. Creating a site - Jeff Thomson (2017-05-11 05:11)
Creating a site... - 1 reply
Read the whole comment and replies

3. error - Priyankar Gayen (2016-12-05 09:22)
error... - 1 reply
Read the whole comment and replies

2. Composer - Robert (2015-12-29 08:37)
Some ideas.... - 5 replies
Read the whole comment and replies

1. Excellent - Sreeprakash. N (2015-12-29 06:01)
Really useful to take a timed snapshot gif... - 0 replies
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (11)   Trackbacks (0)  
  All package blogs All package blogs   PHP Convert Video to GIF PHP Convert Video to GIF   Blog PHP Convert Video to GIF package blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Tutorial to Creat...