You are here

function video_embed_field_handle_youtube_thumbnail in Video Embed Field 7.2

Gets the thumbnail url for youtube videos.

Parameters

string $url: The video URL.

Return value

array The video thumbnail information.

1 string reference to 'video_embed_field_handle_youtube_thumbnail'
video_embed_field_video_embed_handler_info in ./video_embed_field.handlers.inc
Implements hook_video_embed_handler_info().

File

./video_embed_field.handlers.inc, line 214
Provide some handlers for video embed field Other modules can implement the hook_video_embed_handler_info to provide more handlers.

Code

function video_embed_field_handle_youtube_thumbnail($url) {
  $info = array();
  $id = _video_embed_field_get_youtube_id($url);

  // Playlist.
  if (stristr($id, '?list=')) {

    // Strip out all but the ID, including the PL behind the ID.
    $start = strpos($id, '?list=PL') + 8;
    $length = strpos($id, '&') - $start;
    $id = substr($id, $start, $length);
    $info['id'] = $id;

    // Playlist info is stored in XML. The thumbnail is in there.
    $xml = drupal_http_request('http://gdata.youtube.com/feeds/api/playlists/' . $id);
    if (!isset($xml->error)) {
      $xml = new SimpleXMLElement($xml->data);
      $media = $xml
        ->children('http://search.yahoo.com/mrss/');
      if ($media->group->thumbnail && $media->group->thumbnail[0]
        ->attributes()) {
        $attrs = $media->group->thumbnail[0]
          ->attributes();
        $info['url'] = (string) $attrs['url'];
      }
    }
  }
  elseif ($id) {
    $info['id'] = $id;
    $info['url'] = 'http://img.youtube.com/vi/' . $id . '/0.jpg';
  }
  return $info;
}