Posted on : 17-Feb-2010 | By : jontroth | In : PHP Programmer, Tricks & Treats
I needed a quick and simple method to show YouTube thumbnails to for a “What’s New” video section so I whiped up this quick and dirty class.
<?PHP
class YouTubeThumbnailImage {
/* $image_id can be 1, 2, 3 at 120px x 90px or hqdefault at 480px × 360px */
public function get_image_tag( $youtube_url = ”, $image_id = 1, $title = ‘YouTube Video’ ){
if( $image_id > 0 && $image_id < 4 ){
$width = 120;
$height = 90;
}elseif( $image_id == ‘hqdefault’ ){
$width = 480;
$height = 360;
}else{
$image_id = 1
$width = 120;
$height = 90;
}
return ‘<img src=”‘ . $this->image_url( $youtube_url, $image_id ) . ‘” width=”‘ . $width . ‘” height=”‘ . $height . ‘” alt=”‘ . $title . ‘” title=”‘ . $title . ‘” border=”0″ />’;
}
private function get_youtube_id( $youtube_url ){
$url_parts = explode( ‘?v=’, $youtube_url );
if( count( $url_parts ) == 2 ){
$tmp = explode( ‘&’, $url_parts[1] );
if( count( $tmp ) > 1 ){ return $tmp[0]; }else{ return $parts[1]; }
}else{
return $youtube_url;
}
}
private function image_url( $youtube_url, $image_id ){
$youtube_id = $this->get_youtube_id( $youtube_url );
return ‘http://img.youtube.com/vi/’ . $youtube_id . ‘/’ . $image_id . ‘.jpg’;
}
}
?>
$YouTubeThumbnailImage = new YouTubeThumbnailImage();
echo $YouTubeThumbnailImage->get_image_tag( ‘qg4I_I3pfXs’, 1, ‘Test Video Thumbnail’ );
or
echo $YouTubeThumbnailImage->get_image_tag( ‘http://www.youtube.com/watch?v=qg4I_I3pfXs’, 2, ‘Test Video Thumbnail’ );
My cousin recommended this blog and she was totally right keep up the fantastic work!
thank i'm will use in my website
Genial brief and this post helped me alot in my college assignement. Thanks you on your information.