-
Notifications
You must be signed in to change notification settings - Fork 4
/
VideoEmbed.php
executable file
·130 lines (111 loc) · 3.64 KB
/
VideoEmbed.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
<?php
namespace Statamic\Addons\VideoEmbed;
use Statamic\Extend\Extensible;
class VideoEmbed
{
use Extensible;
public function isYouTube($value)
{
return strpos($value, 'youtube') !== false || strpos($value, 'youtu.be') !== false;
}
public function isVimeo($value)
{
return strpos($value, 'vimeo') !== false;
}
public function isValid($value)
{
return ($this->isYouTube($value) || $this->isVimeo($value)) != false;
}
public function getYouTubeVideoId($value)
{
if (strpos($value, '?v=') !== false)
{
parse_str(parse_url($value)['query'], $idstr);
return $idstr['v'];
}
return substr($value, strrpos($value, '/') + 1);
}
public function getVimeoId($value)
{
return substr($value, strrpos($value, '/') + 1);
}
public function getVideoId($value)
{
if ($this->isYouTube($value))
{
return $this->getYouTubeVideoId($value);
}
elseif ($this->isVimeo($value))
{
return $this->getVimeoId($value);
}
return '';
}
public function getVideoSrc($value, $autoplay, $loop, $api, $showinfo, $controls)
{
if (is_null($autoplay)) $autoplay = $this->getConfig('autoplay', false) ? 'true' : 'false';
if (is_null($loop)) $loop = $this->getConfig('loop', false) ? 'true' : 'false';
if (is_null($api)) $api = $this->getConfig('api', false) ? 'true' : 'false';
if (is_null($showinfo)) $showinfo = $this->getConfig('showinfo', true) ? 'true' : 'false';
if (is_null($controls)) $controls = $this->getConfig('controls', true) ? 'true' : 'false';
if ($this->isYouTube($value))
{
return 'https://www.youtube.com/embed/' . $this->getYouTubeVideoId($value) . '?autoplay=' . $autoplay . '&loop=' . $loop . '&enablejsapi=' . $api . '&showinfo=' . $showinfo . '&controls=' . $controls;
}
elseif ($this->isVimeo($value))
{
return 'https://player.vimeo.com/video/' . $this->getVimeoId($value) . '?autoplay=' . $autoplay . '&loop=' . $loop . '&api=' . $api . '&title=' . $showinfo . '&portrait=' . $showinfo . '&byline=' . $showinfo;
}
return '';
}
public function getVideoLink($value)
{
if ($this->isYouTube($value))
{
return 'https://www.youtube.com/watch?v=' . $this->getYouTubeVideoId($value);
}
elseif ($this->isVimeo($value))
{
return 'https://vimeo.com/' . $this->getVimeoId($value);
}
return '';
}
public function getAspectRatio($height, $width)
{
$height = (int)$height;
$width = (int)$width;
$fraction = $width / $height;
if ($fraction >= 2.333)
{
$aspect_ratio = 'cinema';
}
elseif ($fraction >= 1.777)
{
$aspect_ratio = 'wide';
}
elseif ($fraction >= 1.5)
{
$aspect_ratio = 'clasic';
}
elseif ($fraction >= 1.333) {
$aspect_ratio = 'standard';
}
elseif ($fraction > 0.75) {
$aspect_ratio = 'square';
}
elseif ($fraction <= 0.428571429) {
$aspect_ratio = 'portrait_cinema';
}
elseif ($fraction <= 0.5625) {
$aspect_ratio = 'portrait_wide';
}
elseif ($fraction <= 0.665) {
$aspect_ratio = 'portrait_clasic';
} elseif ($fraction <= 0.75) {
$aspect_ratio = 'portrait_standard';
} else {
$aspect_ratio = 'wide';
}
return $aspect_ratio;
}
}