Skip to content

Instantly share code, notes, and snippets.

@gyrus
Created September 10, 2013 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gyrus/6514634 to your computer and use it in GitHub Desktop.
Save gyrus/6514634 to your computer and use it in GitHub Desktop.
Get the URL of a thumbnail for a video on a 3rd-party service
<?php
/**
* Get the URL of a thumbnail for a video on a 3rd-party service
*
* @param string $url Currently supports YouTube and Vimeo
* @return string
*/
function pilau_get_video_thumbnail( $url ) {
$thumb_url = null;
$service = null;
$url_parts = parse_url( $url );
// Which service?
if ( strpos( $url_parts['host'], 'youtube' ) !== false ) {
$service = 'youtube';
} else if ( strpos( $url_parts['host'], 'vimeo' ) !== false ) {
$service = 'vimeo';
}
switch ( $service ) {
case 'youtube': {
$qs_parts = explode( '&', $url_parts['query'] );
foreach ( $qs_parts as $qs_part ) {
$qs_part = explode( '=', $qs_part );
if ( $qs_part[0] = 'v' ) {
$thumb_url = 'http://img.youtube.com/vi/' . $qs_part[1] . '/default.jpg';
}
}
break;
}
case 'vimeo': {
$image = unserialize( file_get_contents( 'http://vimeo.com/api/v2/video/' . $url_parts['query'] . '.php' ) );
$thumb_url = $image[0]['thumbnail_small'];
break;
}
}
return $thumb_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment