Skip to content

Instantly share code, notes, and snippets.

@Phoenix-Effect
Created July 3, 2018 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phoenix-Effect/fecf63d14f916115fafeb615ab7b4989 to your computer and use it in GitHub Desktop.
Save Phoenix-Effect/fecf63d14f916115fafeb615ab7b4989 to your computer and use it in GitHub Desktop.
Given a url of a video hosted on ted, youtube or vimeo, finds where the video is hosted and outputs the thumbnail for that video.
<?php
function outputVideoThumbnail($url){
$parsed_url = parse_url($url);
// if youtube is detected
if($parsed_url['host']=='www.youtube.com') {
$query = $parsed_url['query'];
$Arr = explode('v=',$query);
$videoIDwithString = $Arr[1];
$videoID = substr($videoIDwithString,0,11); ?>
<img src="http://img.youtube.com/vi/<?php echo $videoID; ?>/mqdefault.jpg"> <?php }
// if vimeo is detected
elseif($parsed_url['host']=='vimeo.com') {
$path = $parsed_url['path'];
$ArrV = explode('://vimeo.com/',$path); // from ID to end of the string
$videoID = substr($ArrV[0],1,8); // to get video ID , always length is 8 characters
$vimdeoIDInt = intval($videoID); // ID must be integer
$api_url = 'http://vimeo.com/api/v2/video/' . $videoID . '.php';
$hash = unserialize(@file_get_contents('http://vimeo.com/api/v2/video/' . $api_url . '.php'));
$thumbnail = $hash[0]['thumbnail_large'];
if($thumbnail)
echo '<img src="' . $thumbnail . '">';
else
echo 'no thumbnail';
}
//if ted
elseif($parsed_url['host'] = 'ted.com') {
$end = end(explode('/', $url));
$request = "http://www.ted.com/talks/oembed.json?url=http%3A%2F%2Fwww.ted.com%2Ftalks%2F" . $end;
$response = wp_remote_get( $request );
if( is_wp_error( $response ) ) {
$result = $this->construct_info_retrieval_error( $request, $response );
} else {
$result = json_decode( $response['body'] );
$result = str_replace( '240x180.jpg', '480x360.jpg', $result->thumbnail_url );
}
$results = '<img src="' . $result . '">';
echo $results;
}
else { echo 'Not youtube, ted or vimeo';}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment