-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfunction.video.php
192 lines (175 loc) · 5.41 KB
/
function.video.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Extracts the daily motion id from a daily motion url.
* Returns false if the url is not recognized as a daily motion url.
*/
function getDailyMotionId($url)
{
if (preg_match('!^.+dailymotion\.com/(video|hub)/([^_]+)[^#]*(#video=([^_&]+))?|(dai\.ly/([^_]+))!', $url, $m)) {
if (isset($m[6])) {
return $m[6];
}
if (isset($m[4])) {
return $m[4];
}
return $m[2];
}
return false;
}
/**
* Extracts the vimeo id from a vimeo url.
* Returns false if the url is not recognized as a vimeo url.
*/
function getVimeoId($url)
{
if (preg_match('#(?:https?://)?(?:www.)?(?:player.)?vimeo.com/(?:[a-z]*/)*([0-9]{6,11})[?]?.*#', $url, $m)) {
return $m[1];
}
return false;
}
/**
* Extracts the youtube id from a youtube url.
* Returns false if the url is not recognized as a youtube url.
*/
function getYoutubeId($url)
{
$parts = parse_url($url);
if (isset($parts['host'])) {
$host = $parts['host'];
if (
false === strpos($host, 'youtube') &&
false === strpos($host, 'youtu.be')
) {
return false;
}
}
if (isset($parts['query'])) {
parse_str($parts['query'], $qs);
if (isset($qs['v'])) {
return $qs['v'];
}
else if (isset($qs['vi'])) {
return $qs['vi'];
}
}
if (isset($parts['path'])) {
$path = explode('/', trim($parts['path'], '/'));
return $path[count($path) - 1];
}
return false;
}
/**
* Gets the thumbnail url associated with an url from either:
*
* - youtube
* - daily motion
* - vimeo
*
* Returns false if the url couldn't be identified.
*
* In the case of you tube, we can use the second parameter (format), which
* takes one of the following values:
* - small (returns the url for a small thumbnail)
* - medium (returns the url for a medium thumbnail)
*
*
*
*/
function getVideoThumbnailByUrl($url, $format = 'small')
{
if (false !== ($id = getVimeoId($url))) {
$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$id.php"));
/**
* thumbnail_small
* thumbnail_medium
* thumbnail_large
*/
return $hash[0]['thumbnail_large'];
}
elseif (false !== ($id = getDailyMotionId($url))) {
return 'http://www.dailymotion.com/thumbnail/video/' . $id;
}
elseif (false !== ($id = getYoutubeId($url))) {
/**
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
*
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
* http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
*/
if ('medium' === $format) {
return 'http://img.youtube.com/vi/' . $id . '/hqdefault.jpg';
}
return 'http://img.youtube.com/vi/' . $id . '/default.jpg';
}
return false;
}
/**
* Returns the location of the actual video for a given url which belongs to either:
*
* - youtube
* - daily motion
* - vimeo
*
* Or returns false in case of failure.
* This function can be used for creating video sitemaps.
*/
function getVideoLocation($url)
{
if (false !== ($id = getDailyMotionId($url))) {
return 'http://www.dailymotion.com/embed/video/' . $id;
}
elseif (false !== ($id = getVimeoId($url))) {
return 'http://player.vimeo.com/video/' . $id;
}
elseif (false !== ($id = getYoutubeId($url))) {
return 'http://www.youtube.com/embed/' . $id;
}
return false;
}
/**
* Returns the html code for an embed responsive video, for a given url.
* The url has to be either from:
* - youtube
* - daily motion
* - vimeo
*
* Returns false in case of failure
*/
function getEmbedVideo($url)
{
$code = <<<EEE
<style>
.embed-container {
position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden; max-width: 100%;
}
.embed-container iframe, .embed-container object, .embed-container embed {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
}
</style>
EEE;
if (false !== ($id = getDailyMotionId($url))) {
$code .= <<<EEE
<div class='embed-container'><iframe src='http://www.dailymotion.com/embed/video/$id' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>
EEE;
}
elseif (false !== ($id = getVimeoId($url))) {
$code .= <<<EEE
<div class='embed-container'><iframe src='http://player.vimeo.com/video/$id' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></div>
EEE;
}
elseif (false !== ($id = getYoutubeId($url))) {
$code .= <<<EEE
<div class='embed-container'><iframe src='http://www.youtube.com/embed/$id' frameborder='0' allowfullscreen></iframe></div>
EEE;
}
else {
$code = false;
}
return $code;
}