3

I show a lot of thumbs on my homepage from youtube videos. I was using this function below to grab the thumb from a youtube url which works fast but it doesn't work for url's in the shortned form like youtu.be/JSHDLSKL.

function get_youtube_screen_link( $url = '', $type = 'default', $echo = true ) {
if( empty( $url ) )
    return false;

if( !isset( $type ) )
    $type = '';

$url = esc_url( $url );

preg_match("|[\\?&]v=([^&#]*)|",$url,$vid_id);

if( !isset( $vid_id[1] ) )
    return false;

$img_server_num =  'i'. rand(1,4);

switch( $type ) {
    case 'large':
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/0.jpg";
        break;
    case 'first':
        // Thumbnail of the first frame
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/1.jpg";
        break;
    case 'small':
        // Thumbnail of a later frame(i'm not sure how they determine this)
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/2.jpg";
        break;
    case 'default':
    case '':
    default:
        $img_link = "http://{$img_server_num}.ytimg.com/vi/{$vid_id[1]}/default.jpg";
        break;
}
if( $echo )
    echo $img_link;
else
    return $img_link;

}

So I tried to use Oembed to get the thumbs instead which works for all variations of the youtube url but it retrieves the 480px/360px thumb which causes a lot of cropping to get it down to the 120px/90px size I use to display them. The other issue was it caused my page speed to increase by 4 seconds which Im guessing is a problem with the way I'm implementing it. Here's how I call the thumb inside a loop.

<?php 
 require_once(ABSPATH.'wp-includes/class-oembed.php');
 $oembed= new WP_oEmbed;
 $name = get_post_meta($post->ID,'video_code',true);
 $url = $name;
//As noted in the comments below, you can auto-detect the video provider with the following
 $provider = $oembed->discover($name);
//$provider = 'http://www.youtube.com/oembed';
 $video = $oembed->fetch($provider, $url, array('width' => 300, 'height' => 175));
 $thumb = $video->thumbnail_url; if ($thumb) { ?>
   <img src="<?php echo $thumb; ?>" width="120px" height="90px" />
<?php } ?>

So how should I be doing this to maximize efficiency?

Pollux Khafra
  • 862
  • 5
  • 17
  • 32
  • Use oembed to get the id, and the other part of your first function - or what is your problem? – hakre May 01 '12 at 15:54
  • You mean use oembed to get the standard url and then use the first function to get the thumb? My problem is people submit youtue url's to post videos on my site. The top function won't get the thumb for youtu.be version of the url. Using Oembed to get the thumb gets the 480/360 version which causes unnecessary cropping for me. And it adds load time which I can't figure out why. – Pollux Khafra May 01 '12 at 16:00
  • [Youtube API - Extract video ID](http://stackoverflow.com/questions/6556559/youtube-api-extract-video-id/6556662#6556662) – hakre May 01 '12 at 16:07
  • That doesn't seem to work for a url of the actual video shown in the browser. I'm talking about the long form like `http://www.youtube.com/watch?v=bCDHeEbG3g8&feature=g-all-u` – Pollux Khafra May 01 '12 at 16:24
  • I highly suggest you search the site a bit, because everything you've got a problem with has already been asked and answered - really. You're not the first one who is looking for youtube images (which is no wonder if you take the popularity of that site into account). And keep in mind, if in the future google changes the layout of youtube URIs, all existing solutions might not work any longer. – hakre May 01 '12 at 16:42

2 Answers2

3

I came across this page from youtube explaining their oembed support, They mentioned that they output to json format so I made a function that gets the json data and then enables you to use it.

Feel free to ask if you need more help.

<?php

$youtube_url = 'http://youtu.be/oHg5SJYRHA0'; // url to youtube video

function getJson($youtube_url){

    $baseurl = 'http://www.youtube.com/oembed?url='; // youtube oembed base url
    $url = $baseurl . $youtube_url . '&format=json'; // combines the url with format json

    $json = json_decode(file_get_contents($url)); // gets url and decodes the json

    return $json;

}

$json = getJson($youtube_url);

// from this point on you have all your data placed in variables.

$provider_url = $json->{'provider_url'};
$thumbnail_url = $json->{'thumbnail_url'};
$title = $json->{'title'};
$html = $json->{'html'};
$author_name = $json->{'author_name'};
$height = $json->{'height'};
$thumbnail_width = $json->{'thumbnail_width'};
$thumbnail_height = $json->{'thumbnail_height'};
$width = $json->{'width'};
$version = $json->{'version'};
$author_url = $json->{'author_url'};
$provider_name = $json->{'provider_name'};
$type = $json->{'type'};

echo '<img src="'.$thumbnail_url.'" />'; // echo'ing out the thumbnail image
Nick Fury
  • 1,313
  • 3
  • 13
  • 23
0

Ok I came up with a solution from pieces of other questions. First we need to get the id from any type of url youtube has using this function.

function getVideoId($url)
{
$parsedUrl = parse_url($url);
if ($parsedUrl === false)
    return false;

if (!empty($parsedUrl['query']))
{
    $query = array();
    parse_str($parsedUrl['query'], $query);
    if (!empty($query['v']))
        return $query['v'];
}

if (strtolower($parsedUrl['host']) == 'youtu.be')
    return trim($parsedUrl['path'], '/');

return false;
}

Now we can get use YouTube Data API to get the thumbnail from the video id. Looks like this.

<?php
  $vid_id = getVideoId($video_code);
  $json = json_decode(file_get_contents("http://gdata.youtube.com/feeds/api/videos/$vid_id?v=2&alt=jsonc"));
  echo '<img src="' . $json->data->thumbnail->sqDefault . '" width="176" height="126">'; 
?>

The problem is that is causing an extra 2 seconds load time so I simply use the $vid_id and place it inside http://i3.ytimg.com/vi/<?php echo $vid_id; ?>/default.jpg which gets rid of the 2 seconds added by accessing the youtube api.

Pollux Khafra
  • 862
  • 5
  • 17
  • 32