Friday, March 9, 2012

get thumbnail image from youtube,url,embed code (both type of embed code old and new iframe)

0
want to get the image from youtube embed code (new and old) and from youtube video url this function will help you ,

 function video_image($youtube_code)
{
    // get the video code if this is an embed code    (old embed)
    preg_match('/youtube\.com\/v\/([\w\-]+)/', $youtube_code, $match);

    // if old embed returned an empty ID, try capuring the ID from the new iframe embed
    if($match[1] == '')
        preg_match('/youtube\.com\/embed\/([\w\-]+)/', $youtube_code, $match);

    // if it is not an embed code, get the video code from the youtube URL    
    if($match[1] == '')
        preg_match('/v\=(.+)&/',$youtube_code ,$match);
    
     if($match[1] == '')
        preg_match('/v\=(.+)/',$youtube_code ,$match);
    // get the corresponding thumbnail images    
    $full_size_thumbnail_image['small'] = "http://img.youtube.com/vi/".$match[1]."/0.jpg";
    $full_size_thumbnail_image['large'] = "http://img.youtube.com/vi/".$match[1]."/1.jpg";
    $full_size_thumbnail_image['medium'] = "http://img.youtube.com/vi/".$match[1]."/2.jpg";
    $full_size_thumbnail_image['smallmedium'] = "http://img.youtube.com/vi/".$match[1]."/3.jpg";

    // return whichever thumbnail image you would like to retrieve
    return $full_size_thumbnail_image;        
}

print_r(video_image('youtube embed code, url wil come here'));

?>

No comments :

Post a Comment