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
8
9
/* Get video ID from $v (if required) */
10
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:
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
8
9
/* Get video ID from $v (if required) */
10
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.