php - How can I show youtube thumbnails on my site instead of the embedded video -
this question has answer here:
- how youtube video thumbnail youtube api? 23 answers
i have cms , admin can link show video entering title , embed code saved on database.
when site showing list of videos shows videos in embed form. there way make these embed video scripts become thumbnails use less bandwidth
here's function wrote grab various bits of youtube information wordpress cms, php.
in order use it, have extract video code url. i'm including functionality used that.
please note, didn't mention cms you're using. commented in code i'm getting url originally; since i'm using wordpress , advanced custom fields, i'm grabbing url acf subfield. can pass in need. :)
// grab video's url cms. // get_sub_field() advanced custom fields function wordpress // make sure swap out plan on passing in // url can formed http://www.youtube.com/watch?v=9bzkp7q19f0 // note didn't include functionality http://youtu.be type of urls, // work out. :) in web app, have similar vimeo function, // why bother check url in first place. $video_url = get_sub_field('cms_video_url'); $video_url_a = parse_url($video_url); // check if youtube video. add in youtu.be logic here. if($video_url_a['host'] == 'www.youtube.com' || $video_url_a['host'] == 'youtube.com'){ $array = explode("&", $video_url_a['query']); $video_id = substr($array[0],2); // grab info large thumbnail. grab small thumb, // title, description, author, or author's uri. // see get_youtube_info() function below $videothumb = get_youtube_info($video_id, 'thumbnail_large'); // here's example of grabbing video title alt tag. :) $videotitle = get_youtube_info($video_id, 'title'); } else { // enter whatever fail functionality want } echo '<img class="video-thumb" src="' . $videothumb . '" alt="' . $videotitle . '" />' and here's get_youtube_info() function:
/* * here's function limited set of youtube info * see switch in function * example json returned: gungnam style! * http://gdata.youtube.com/feeds/api/videos/9bzkp7q19f0?v=2&alt=json-in-script&prettyprint=true */ function get_youtube_info ( $vid, $info ) { $youtube = "http://gdata.youtube.com/feeds/api/videos/$vid?v=2&alt=json&feature=related"; $ch = curl_init($youtube); curl_setopt($ch, curlopt_header, false); curl_setopt($ch, curlopt_returntransfer, true); $output = curl_exec($ch); curl_close($ch); //if $assoc = true doesn't work, try: //$output = json_decode($output, true); $output = json_decode($output, $assoc = true); //add ['feed'] in if exists. if ($output['feed']) { $path = &$output['feed']['entry']; } else { $path = &$output['entry']; } //set switch return various data bits return. switch($info) { case 'title': $output = $path['title']['$t']; break; case 'description': $output = $path['media$group']['media$description']['$t']; break; case 'author': $output = $path['author'][0]['name']; break; case 'author_uri': $output = $path['author'][0]['uri']; break; case 'thumbnail_small': $output = $path['media$group']['media$thumbnail'][0]['url']; break; case 'thumbnail_medium': $output = $path['media$group']['media$thumbnail'][2]['url']; break; case 'thumbnail_large': $output = $path['media$group']['media$thumbnail'][3]['url']; break; default: return $output; break; } return $output; }
Comments
Post a Comment