This package makes it easier to work with both Youtube and Vimeo video url's. It automatically detects the reference from the url.
For example:
https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://youtu.be/dQw4w9WgXcQ
https://www.youtube.com/embed/dQw4w9WgXcQ
are all valid youtube links, but to embed an iFrame, you need the third option.
This package allows all the options as input, and is able to generate the correct output urls to embed the iframe.
You can install the package with composer
composer require justijndepover/embed-video
use Justijndepover\EmbedVideo\Video;
$video = Video::from('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
If you provide the class with a faulty link, a Justijndepover\EmbedVideo\VideoException
will be thrown.
$validated = Video::validate('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
This will validate the given url and return either true
or false
$html = $video->embed();
This will generate the embedded iframe for either Youtube or Vimeo.
<iframe type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=0&rel=0" frameborder="0"></iframe>
$url = $video->embedUrl();
This will generate the embedded url used in the iframe for either Youtube or Vimeo.
https://www.youtube.com/embed/dQw4w9WgXcQ
$thumbnail = $video->thumbnail();
This will generate a thumbnail url for the cover image;
http://img.youtube.com/vi/dQw4w9WgXcQ/0.jpg
$reference = $video->reference();
This will return the video reference;
dQw4w9WgXcQ
To add autoplay to the embed iframe, you can make use of the fluent syntax:
$html = $video->autoplay()->embed();
<iframe type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1&rel=0" frameborder="0"></iframe>
To add a class to the embed iframe, you can make use of the fluent syntax:
$html = $video->class('video-container')->embed();
<iframe class="video-container" type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=0&rel=0" frameborder="0"></iframe>
To add additional attributes to the embed iframe, you can make use of the fluent syntax:
$html = $video->addAttribute('width', 'auto')->embed();
<iframe type="text/html" src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=0&rel=0" frameborder="0" width="auto"></iframe>
If you find any security related issues, please open an issue or contact me directly at justijndepover@gmail.com.
If you wish to make any changes or improvements to the package, feel free to make a pull request.
The MIT License (MIT). Please see License File for more information.