Preview for Videos and images / Video resolution selection

This commit is contained in:
Michael 2021-04-28 19:05:46 +00:00
parent 909e029168
commit 3d64c3031b
9 changed files with 62 additions and 35 deletions

View file

@ -448,9 +448,10 @@ class Media
* Split the attachment media in the three segments "visual", "link" and "additional"
*
* @param int $uri_id
* @param string $guid
* @return array attachments
*/
public static function splitAttachments(int $uri_id)
public static function splitAttachments(int $uri_id, string $guid = '')
{
$attachments = ['visual' => [], 'link' => [], 'additional' => []];
@ -459,6 +460,9 @@ class Media
return $attachments;
}
$height = 0;
$selected = '';
foreach ($media as $medium) {
$type = explode('/', current(explode(';', $medium['mimetype'])));
if (count($type) < 2) {
@ -478,11 +482,31 @@ class Media
continue;
}
if (in_array($medium['type'], [self::AUDIO, self::VIDEO, self::IMAGE]) ||
in_array($filetype, ['audio', 'video', 'image'])) {
if (in_array($medium['type'], [self::AUDIO, self::IMAGE]) ||
in_array($filetype, ['audio', 'image'])) {
$attachments['visual'][] = $medium;
} elseif (($medium['type'] == self::VIDEO) || ($filetype == 'video')) {
if (strpos($medium['url'], $guid) !== false) {
// Peertube videos are delivered in many different resolutions. We pick a moderate one.
// By checking against the GUID we also ensure to only work this way on Peertube posts.
// This wouldn't be executed when someone for example on Mastodon was sharing multiple videos in a single post.
if (empty($height) || ($height > $medium['height']) && ($medium['height'] >= 480)) {
$height = $medium['height'];
$selected = $medium['url'];
}
$video[$medium['url']] = $medium;
} else {
$attachments['visual'][] = $medium;
}
} else {
$attachments['additional'][] = $medium;
$attachments['additional'][] = $medium;
}
}
if (!empty($selected)) {
$attachments['visual'][] = $video[$selected];
unset($video[$selected]);
foreach ($video as $element) {
$attachments['additional'][] = $element;
}
}
return $attachments;