From f8a22a1b25602697d471be6f69dcb44390bd5c25 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 24 Apr 2018 15:08:59 +0200 Subject: [PATCH 1/2] Add MIME type to video tag --- system/src/Grav/Common/Page/Medium/VideoMedium.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/system/src/Grav/Common/Page/Medium/VideoMedium.php b/system/src/Grav/Common/Page/Medium/VideoMedium.php index b3c8eed852..e36ccd71ee 100644 --- a/system/src/Grav/Common/Page/Medium/VideoMedium.php +++ b/system/src/Grav/Common/Page/Medium/VideoMedium.php @@ -22,10 +22,13 @@ class VideoMedium extends Medium 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); return [ 'name' => 'video', - 'text' => 'Your browser does not support the video tag.', + 'text' => 'Your browser does not support the video tag.', 'attributes' => $attributes ]; } From 2d4542ec70f94d5ad94fffd733c63d52549e3052 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 27 Apr 2018 22:41:18 +0200 Subject: [PATCH 2/2] Automatically detect video thumbnail The image file will be added as poster attribute to the video tag. Looks for the following files in the video folder: - $filename.jpg - $filename.png - $filename.$ext.thumb.jpg - $filename.$ext.thumb.png The code is hacky but works for me, and I don't know the Grav API good enough to do it better. --- .../Grav/Common/Page/Medium/VideoMedium.php | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/system/src/Grav/Common/Page/Medium/VideoMedium.php b/system/src/Grav/Common/Page/Medium/VideoMedium.php index e36ccd71ee..6639cfc419 100644 --- a/system/src/Grav/Common/Page/Medium/VideoMedium.php +++ b/system/src/Grav/Common/Page/Medium/VideoMedium.php @@ -26,6 +26,13 @@ protected function sourceParsedownElement(array $attributes, $reset = true) $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' => 'Your browser does not support the video tag.', @@ -33,6 +40,46 @@ protected function sourceParsedownElement(array $attributes, $reset = true) ]; } + /** + * 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 *