Thursday, April 26, 2012

[YouTube-API] How to get the thumb of a video for youtube.be url.

Awhile back I found the code below that gets the thumb of a video from a submitted url. The problem is that it works fine for every type of url but the youtube.be shortened version. What do I change to this code to make it grab the thumb of the youtube.be version aswell as the other versions?

   <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("|[\\?&]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;

}
</code>

--
You received this message because you are subscribed to the Google Groups "YouTube APIs Developer Forum" group.
To view this discussion on the web visit https://groups.google.com/d/msg/youtube-api-gdata/-/a96Ly0w626QJ.
To post to this group, send email to youtube-api-gdata@googlegroups.com.
To unsubscribe from this group, send email to youtube-api-gdata+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/youtube-api-gdata?hl=en.

No comments:

Post a Comment