title: How to correctly use oembed to pull thumbs from youtube
body: <p>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.</p>
  
  <pre><code>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("|[\\?&amp;]v=([^&amp;#]*)|",$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;
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>&lt;?php 
   require_once(ABSPATH.'wp-includes/class-oembed.php');
   $oembed= new WP_oEmbed;
   $name = get_post_meta($post-&gt;ID,'video_code',true);
   $url = $name;
  //As noted in the comments below, you can auto-detect the video provider with the following
   $provider = $oembed-&gt;discover($name);
  //$provider = 'http://www.youtube.com/oembed';
   $video = $oembed-&gt;fetch($provider, $url, array('width' =&gt; 300, 'height' =&gt; 175));
   $thumb = $video-&gt;thumbnail_url; if ($thumb) { ?&gt;
     &lt;img src="&lt;?php echo $thumb; ?&gt;" width="120px" height="90px" /&gt;
  &lt;?php } ?&gt;
  </code></pre>
  
  <p>So how should I be doing this to maximize efficiency?     </p>