Created
July 9, 2012 21:17
-
-
Save dfadler/3079004 to your computer and use it in GitHub Desktop.
YouTube or Vimeo Abstraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
// Example... | |
* $args = array( | |
* 'url' => 'http://vimeo.com/26202998', | |
* 'height' => 315, | |
* 'width' => 420, | |
* 'wmode' => 'window', | |
* 'autoplay' => 1, | |
* 'loop' => 1 | |
* ); | |
* $iframe_video = ob_insert_video($args); | |
* echo $iframe_video; | |
*/ | |
/** | |
* ob_insert_video | |
* | |
* @param array $params | |
* @return $video; | |
* @author fadlerd | |
*/ | |
function ob_insert_video($params) { | |
// Defaults settings | |
$settings = array( | |
'height' => 200, // YouTube minimum height | |
'width' => 200, // YouTube minimum width | |
'url' => '', // The url of the video | |
'autoplay' => 0, // A setting of 1 will cause the video to autoplay | |
'jsapi' => 0, // A setting of 1 will enable the javascript api for either service | |
'loop' => 0, // A setting of 1 will cause the video to continuously play | |
'wmode' => 'window' // Window is the default wmode for more information http://helpx.adobe.com/flash/kb/flash-object-embed-tag-attributes.html | |
); | |
// Merges defaults with user provided params | |
$settings = array_merge($settings, $params); | |
// Laziness is a virtue | |
$height = $settings['height']; | |
$width = $settings['width']; | |
$url = $settings['url']; | |
$autoplay = 'autoplay='.$settings['autoplay']; | |
$jsapi = preg_match("/youtube.com/", $url) ? 'enablejsapi='.$settings['jsapi'] : 'api='.$settings['jsapi']; | |
$loop = 'loop='.$settings['loop']; | |
$wmode = 'wmode='.$settings['wmode']; | |
// Check to see if a url has been provided | |
if(strlen($settings['url']) === 0) { | |
// If no url has been provided alert then return... | |
echo '<h1>Video url must be provided</h1>'; | |
return; | |
} | |
/** | |
* parse_video_url | |
* | |
* @param string $video_url | |
* @return Video url used in iframe or no url and return... | |
* @author fadlerd | |
*/ | |
function parse_video_url($video_url){ | |
// If url is YouTube | |
if(preg_match("/youtube.com/", $video_url)) { | |
// Parse the url and store it in an array | |
$you_tube_url = parse_url($video_url); | |
// Split the query string at v= | |
$you_tube_url = explode('v=', $you_tube_url['query']); | |
// Return video id | |
return "http://www.youtube.com/embed/$you_tube_url[1]"; | |
// "<iframe src='http://www.youtube.com/embed/zM2PNRwQcdM' width='560' height='315' frameborder='0' allowfullscreen></iframe>" | |
// If url is Vimeo | |
} else if(preg_match("/vimeo.com/", $video_url)) { | |
// Need to look into webkitAllowFullScreen mozallowfullscreen | |
// Parse the url and store it in an array | |
$vimeo_url = parse_url($video_url); | |
// Split the path at / | |
$vimeo_url = explode('/', $vimeo_url['path']); | |
// Return video id | |
return "http://player.vimeo.com/video/$vimeo_url[1]"; | |
// "<iframe src='http://player.vimeo.com/video/41261299' width='500' height='281' frameborder='0' allowFullScreen webkitAllowFullScreen mozallowfullscreen ></iframe>" | |
// Will eventually support http://mediaelementjs.com/ | |
// It will eventually be possible to wrap the YouTube and Vimeo player | |
// http://www.mediaelementjs.com/examples/?name=youtube | |
} else { | |
echo '<h1>Video service not supported</h1>'; | |
return; | |
} | |
} | |
// The url for the iframe | |
$url = parse_video_url($url); | |
// Appends wmode to url | |
$url .= '?'.$wmode; | |
// Appends autoplay to url | |
$url .= '&'.$autoplay; | |
// Appends loop to url | |
$url .= '&'.$loop; | |
// Appends javascript api | |
$url .= '&'.$jsapi; | |
// Configures the iframe | |
$video = "<iframe src='$url' width='$width' height='$width' frameborder='0' allowfullscreen></iframe>"; | |
// Return the video | |
return $video; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment