Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video autoposter #2000

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion system/src/Grav/Common/Page/Medium/VideoMedium.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,64 @@ 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);

if (!isset($attributes['poster'])) {
$poster = $this->findPoster($location);
if ($poster) {
$attributes['poster'] = $poster;
}
}

return [
'name' => 'video',
'text' => '<source src="' . $location . '">Your browser does not support the video tag.',
'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
*
Expand Down