Skip to content

Instantly share code, notes, and snippets.

@fikrirasyid
Created September 6, 2014 17:35
Show Gist options
  • Save fikrirasyid/9d1703d5d41325bf2ec5 to your computer and use it in GitHub Desktop.
Save fikrirasyid/9d1703d5d41325bf2ec5 to your computer and use it in GitHub Desktop.
PHP Code Snippets: Working With YouTube
/**
* Get YouTube Video ID by YouTube URL
*
* @param string YouTube video URL
*
* @return string YouTube video ID
*/
function get_youtube_id_by_url( $url ){
// Parse URL
$parsed_url = parse_url($url);
// Get query string values
parse_str($parsed_url['query'], $querystrings);
// Return video ID
return $querystrings['v'];
}
/**
* Get YouTube video thumb based on YouTube video ID
*
* @param string YouTube video id
* @param string YouTube thumb size
*
* @return string YouTube video thumb
*/
function get_youtube_thumb_by_id( $video_id, $size = 'default' ){
switch($size){
case 'high':
$resolution = 'hqdefault';
break;
case 'middle':
$resolution = 'mqdefault';
break;
default :
$resolution = 'default';
break;
}
return "http://img.youtube.com/vi/$video_id/$resolution.jpg";
}
/**
* Get YouTube thumb by url
*
* @param string YouTube video URL
* @param string YouTube video thumb size
*
* @return string YouTube video thumb
*/
function get_youtube_thumb_by_url( $url, $size = 'default' ){
return get_youtube_thumb_by_id( get_youtube_id_by_url( $url ), $size );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment