Created
September 10, 2013 19:49
-
-
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
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 | |
/** | |
* 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