RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.74% (6.40%*) • Investment PI: 5.49% (6.32%*)

Get YouTube Thumbnail With PHP

This basic function is provided as a reference for our team. We generally don't use it; instead, we retrieve the highest quality thumbnail and scale it as required with another library. If used, a check should be made to ensure the image exists and, if it doesn't, an alternative should be returned.

1
<?php 
2
/*
3
 Return YouTube thumbnail
4
 http://www.beliefmedia.com/code/php-snippets/youtube-thumbnail-url
5
*/
6
 
7
function beliefmedia_youtube_thumbnail($v, $size = '0') {
8
 
9
  /* Get video ID from $v (if required) */
10
  if ( ($v != '') && (strpos($v, 'http') !== false) ) {
11
   preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\??n]+|(?<=youtu.be/)[^&\n]+#", $v, $matches);
12
   $v = $matches['0'];
13
  }
14
 
15
  switch ($size) {
16
 
17
     case 0:
18
        /* Player background, 480 x 360 */
19
        $url = 'https://img.youtube.com/vi/' . $v . '/0.jpg';
20
        break;
21
    case 1:
22
        /* Start thumbnail, 120 x 90 */
23
        $url = 'https://img.youtube.com/vi/' . $v . '/1.jpg';
24
        break;
25
    case 2:
26
        /* Middle thumbnail, 120 x 90 */
27
        $url = 'https://img.youtube.com/vi/' . $v . '/2.jpg';
28
        break;
29
    case 3:
30
        /* End thumbnail, 120 x 90 */
31
        $url = 'https://img.youtube.com/vi/' . $v . '/3.jpg';
32
        break;
33
    case 4:
34
        /* High Quality Thumbnail, 480 x 360 */
35
        $url = 'https://img.youtube.com/vi/' . $v . '/hqdefault.jpg';
36
        break;
37
    case 5:
38
        /* Medium Quality Thumbnail, 320 x 180 */
39
        $url = 'https://img.youtube.com/vi/' . $v . '/mqdefault.jpg';
40
        break;
41
    case 6:
42
        /* Normal Quality Thumbnail, 120 x 90 */
43
        $url = 'https://img.youtube.com/vi/' . $v . '/default.jpg';
44
        break;
45
 
46
    /* High Quality Thumbnails */
47
 
48
    case 7:
49
        /* Standard Definition Thumbnail, 640 x 480 */
50
        $url = 'https://img.youtube.com/vi/' . $v . '/sddefault.jpg';
51
        break;
52
    case 8:
53
        /* Maximum Resolution Thumbnail, 1920 x 1080 */
54
        $url = 'https://img.youtube.com/vi/' . $v . '/maxresdefault.jpg';
55
        break;
56
 
57
  }
58
 
59
 return $url;
60
}

Usage:

1
<?php 
2
/* Return YouTube Thumb */
3
echo beliefmedia_youtube_thumbnail('vbxl3hc_pL8', $size = '8');

Return Array of Data

The second function is to support a couple of articles on our website. The purpose of the function is to return an array of information relating to the image.

1
<?php 
2
/*
3
 Return YouTube thumbnail
4
 http://www.beliefmedia.com/code/php-snippets/youtube-thumbnail-url
5
*/
6
 
7
function beliefmedia_youtube_thumbnail($v, $size = '0') {
8
 
9
  /* Get video ID from $v (if required) */
10
  if ( ($v != '') && (strpos($v, 'http') !== false) ) {
11
   preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=embed/)[^&\n]+|(?<=v=)[^&\??n]+|(?<=youtu.be/)[^&\n]+#", $v, $matches);
12
   $v = $matches['0'];
13
  }
14
 
15
  $video = array();
16
 
17
  switch ($size) {
18
 
19
     case 0:
20
        /* Player background, 480 x 360 */
21
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/0.jpg';
22
        $video['width'] = '480';
23
        $video['height'] = '360';
24
        break;
25
    case 1:
26
        /* Start thumbnail, 120 x 90 */
27
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/1.jpg';
28
        $video['width'] = '120';
29
        $video['height'] = '90';
30
        break;
31
    case 2:
32
        /* Middle thumbnail, 120 x 90 */
33
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/2.jpg';
34
        $video['width'] = '120';
35
        $video['height'] = '90';
36
        break;
37
    case 3:
38
        /* End thumbnail, 120 x 90 */
39
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/3.jpg';
40
        $video['width'] = '120';
41
        $video['height'] = '90';
42
        break;
43
    case 4:
44
        /* High Quality Thumbnail, 480 x 360 */
45
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/hqdefault.jpg';
46
        $video['width'] = '480';
47
        $video['height'] = '360';
48
        break;
49
    case 5:
50
        /* Medium Quality Thumbnail, 320 x 180 */
51
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/mqdefault.jpg';
52
        $video['width'] = '320';
53
        $video['height'] = '180';
54
        break;
55
    case 6:
56
        /* Normal Quality Thumbnail, 120 x 90 */
57
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/default.jpg';
58
        $video['width'] = '120';
59
        $video['height'] = '90';
60
        break;
61
 
62
    /* High Quality Thumbnails */
63
 
64
    case 7:
65
        /* Standard Definition Thumbnail, 640 x 480 */
66
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/sddefault.jpg';
67
        $video['width'] = '640';
68
        $video['height'] = '480';
69
        break;
70
    case 8:
71
        /* Maximum Resolution Thumbnail, 1920 x 1080 */
72
        $video['url'] = 'https://img.youtube.com/vi/' . $v . '/maxresdefault.jpg';
73
        $video['width'] = '1920';
74
        $video['height'] = '1080';
75
        break;
76
 
77
  }
78
 
79
 return $video;
80
}

The API should generally be used to retrieve available thumbnails. Usage of the API negates the need to check if an image exists.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment