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