-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathVideoMedium.php
More file actions
160 lines (139 loc) · 3.67 KB
/
VideoMedium.php
File metadata and controls
160 lines (139 loc) · 3.67 KB
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
<?php
/**
* @package Grav.Common.Page
*
* @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Common\Page\Medium;
class VideoMedium extends Medium
{
use StaticResizeTrait;
/**
* Parsedown element for source display mode
*
* @param array $attributes
* @param boolean $reset
* @return array
*/
protected function sourceParsedownElement(array $attributes, $reset = true)
{
$location = $this->url($reset);
$path = parse_url($location, PHP_URL_PATH);
$extension = pathinfo($path, PATHINFO_EXTENSION);
$mimeType = \Grav\Common\Utils::getMimeByExtension($extension);
if (!isset($attributes['poster'])) {
$poster = $this->findPoster($location);
if ($poster) {
$attributes['poster'] = $poster;
}
}
return [
'name' => 'video',
'text' => '<source src="' . $location . '" type="' . $mimeType . '">Your browser does not support the video tag.',
'attributes' => $attributes
];
}
/**
* Try to find a poster image for the video.
*
* Looks at the same location as the video file is:
* - $filename.jpg
* - $filename.png
* - $filename.$ext.thumb.jpg
* - $filename.$ext.thumb.png
*
* @param string $location Video location
*
* @return string Poster URL or false
*/
protected function findPoster($location)
{
$noQuery = preg_replace('#\\?.*$#', '', $location);
$fullPath = GRAV_ROOT . $noQuery;
if (!file_exists($fullPath)) {
return false;
}
$parts = pathinfo($fullPath);
$noExt = $parts['dirname'] . DIRECTORY_SEPARATOR . $parts['filename'];
$possibilities = [
$noExt . '.jpg',
$noExt . '.png',
$fullPath . '.thumb.jpg',
$fullPath . '.thumb.png',
];
foreach ($possibilities as $thumbLocation) {
if (file_exists($thumbLocation)) {
$relative = substr($thumbLocation, strlen(GRAV_ROOT));
return $relative;
}
}
}
/**
* Allows to set or remove the HTML5 default controls
*
* @param bool $display
* @return $this
*/
public function controls($display = true)
{
if($display) {
$this->attributes['controls'] = true;
} else {
unset($this->attributes['controls']);
}
return $this;
}
/**
* Allows to set the video's poster image
*
* @param $urlImage
* @return $this
*/
public function poster($urlImage)
{
$this->attributes['poster'] = $urlImage;
return $this;
}
/**
* Allows to set the loop attribute
*
* @param bool $status
* @return $this
*/
public function loop($status = false)
{
if($status) {
$this->attributes['loop'] = true;
} else {
unset($this->attributes['loop']);
}
return $this;
}
/**
* Allows to set the autoplay attribute
*
* @param bool $status
* @return $this
*/
public function autoplay($status = false)
{
if($status) {
$this->attributes['autoplay'] = true;
} else {
unset($this->attributes['autoplay']);
}
return $this;
}
/**
* Reset medium.
*
* @return $this
*/
public function reset()
{
parent::reset();
$this->attributes['controls'] = true;
return $this;
}
}