posthandler.php: format

This commit is contained in:
Zankaria 2024-09-12 10:37:33 +02:00
parent f70d70d1db
commit cd00809040

View file

@ -1,82 +1,93 @@
<?php <?php
// Glue code for handling a Tinyboard post. // Glue code for handling a Tinyboard post.
// Portions of this file are derived from Tinyboard code. // Portions of this file are derived from Tinyboard code.
function postHandler($post) { function postHandler($post) {
global $board, $config; global $board, $config;
if ($post->has_file) foreach ($post->files as &$file) if ($file->extension == 'webm' || $file->extension == 'mp4') {
if ($config['webm']['use_ffmpeg']) { if ($post->has_file) {
require_once dirname(__FILE__) . '/ffmpeg.php'; foreach ($post->files as &$file) {
$webminfo = get_webm_info($file->file_path); if ($file->extension == 'webm' || $file->extension == 'mp4') {
if (empty($webminfo['error'])) { if ($config['webm']['use_ffmpeg']) {
$file->width = $webminfo['width']; require_once dirname(__FILE__) . '/ffmpeg.php';
$file->height = $webminfo['height'];
if ($config['spoiler_images'] && isset($_POST['spoiler'])) { $webminfo = get_webm_info($file->file_path);
$file = webm_set_spoiler($file); if (empty($webminfo['error'])) {
} $file->width = $webminfo['width'];
else { $file->height = $webminfo['height'];
$file = set_thumbnail_dimensions($post, $file); if ($config['spoiler_images'] && isset($_POST['spoiler'])) {
$tn_path = $board['dir'] . $config['dir']['thumb'] . $file->file_id . '.jpg'; $file = webm_set_spoiler($file);
if(0 == make_webm_thumbnail($file->file_path, $tn_path, $file->thumbwidth, $file->thumbheight, $webminfo['duration'])) { } else {
$file->thumb = $file->file_id . '.jpg'; $file = set_thumbnail_dimensions($post, $file);
} $tn_path = $board['dir'] . $config['dir']['thumb'] . $file->file_id . '.jpg';
else {
$file->thumb = 'file'; if (0 == make_webm_thumbnail($file->file_path, $tn_path, $file->thumbwidth, $file->thumbheight, $webminfo['duration'])) {
} $file->thumb = $file->file_id . '.jpg';
} } else {
} $file->thumb = 'file';
else { }
return $webminfo['error']['msg']; }
} } else {
} return $webminfo['error']['msg'];
else { }
require_once dirname(__FILE__) . '/videodata.php'; } else {
$videoDetails = videoData($file->file_path); require_once dirname(__FILE__) . '/videodata.php';
if (!isset($videoDetails['container']) || $videoDetails['container'] != 'webm') return "not a WebM file"; $videoDetails = videoData($file->file_path);
// Set thumbnail if (!isset($videoDetails['container']) || $videoDetails['container'] != 'webm') {
$thumbName = $board['dir'] . $config['dir']['thumb'] . $file->file_id . '.webm'; return "not a WebM file";
if ($config['spoiler_images'] && isset($_POST['spoiler'])) { }
// Use spoiler thumbnail
$file = webm_set_spoiler($file); // Set thumbnail
} elseif (isset($videoDetails['frame']) && $thumbFile = fopen($thumbName, 'wb')) { $thumbName = $board['dir'] . $config['dir']['thumb'] . $file->file_id . '.webm';
// Use single frame from video as pseudo-thumbnail if ($config['spoiler_images'] && isset($_POST['spoiler'])) {
fwrite($thumbFile, $videoDetails['frame']); // Use spoiler thumbnail.
fclose($thumbFile); $file = webm_set_spoiler($file);
$file->thumb = $file->file_id . '.webm'; } elseif (isset($videoDetails['frame']) && $thumbFile = fopen($thumbName, 'wb')) {
} else { // Use single frame from video as pseudo-thumbnail.
// Fall back to file thumbnail fwrite($thumbFile, $videoDetails['frame']);
$file->thumb = 'file'; fclose($thumbFile);
} $file->thumb = $file->file_id . '.webm';
unset($videoDetails['frame']); } else {
// Set width and height // Fall back to file thumbnail.
if (isset($videoDetails['width']) && isset($videoDetails['height'])) { $file->thumb = 'file';
$file->width = $videoDetails['width']; }
$file->height = $videoDetails['height']; unset($videoDetails['frame']);
if ($file->thumb != 'file' && $file->thumb != 'spoiler') {
$file = set_thumbnail_dimensions($post, $file); // Set width and height
} if (isset($videoDetails['width']) && isset($videoDetails['height'])) {
} $file->width = $videoDetails['width'];
} $file->height = $videoDetails['height'];
} if ($file->thumb != 'file' && $file->thumb != 'spoiler') {
$file = set_thumbnail_dimensions($post, $file);
}
}
}
}
}
}
} }
function set_thumbnail_dimensions($post,$file) {
global $board, $config; function set_thumbnail_dimensions($post, $file) {
$tn_dimensions = array(); global $config;
$tn_maxw = $post->op ? $config['thumb_op_width'] : $config['thumb_width'];
$tn_maxh = $post->op ? $config['thumb_op_height'] : $config['thumb_height']; $tn_maxw = $post->op ? $config['thumb_op_width'] : $config['thumb_width'];
if ($file->width > $tn_maxw || $file->height > $tn_maxh) { $tn_maxh = $post->op ? $config['thumb_op_height'] : $config['thumb_height'];
$file->thumbwidth = min($tn_maxw, intval(round($file->width * $tn_maxh / $file->height))); if ($file->width > $tn_maxw || $file->height > $tn_maxh) {
$file->thumbheight = min($tn_maxh, intval(round($file->height * $tn_maxw / $file->width))); $file->thumbwidth = min($tn_maxw, intval(round($file->width * $tn_maxh / $file->height)));
} else { $file->thumbheight = min($tn_maxh, intval(round($file->height * $tn_maxw / $file->width)));
$file->thumbwidth = $file->width; } else {
$file->thumbheight = $file->height; $file->thumbwidth = $file->width;
} $file->thumbheight = $file->height;
return $file; }
return $file;
} }
function webm_set_spoiler($file) { function webm_set_spoiler($file) {
global $board, $config; global $config;
$file->thumb = 'spoiler';
$size = @getimagesize($config['spoiler_image']); $file->thumb = 'spoiler';
$file->thumbwidth = $size[0]; $size = @getimagesize($config['spoiler_image']);
$file->thumbheight = $size[1]; $file->thumbwidth = $size[0];
return $file; $file->thumbheight = $size[1];
return $file;
} }