Add image spoilering

main
Pk11 2 years ago
parent 021a8ee120
commit fb21bf1d7e

@ -24,7 +24,8 @@ this is a silly little BBS page I've thrown together out of a desite to have som
comment VARCHAR(2048), comment VARCHAR(2048),
img VARCHAR(256), img VARCHAR(256),
thumb_width INT, thumb_width INT,
thumb_height INT thumb_height INT,
spoiler BOOL
); );
``` ```
```sql ```sql

@ -61,7 +61,10 @@
* trip VARCHAR(10), * trip VARCHAR(10),
* email VARCHAR(256), * email VARCHAR(256),
* comment VARCHAR(2048), * comment VARCHAR(2048),
* img VARCHAR(256) * img VARCHAR(256),
* thumb_width INT,
* thumb_height INT,
* spoiler BOOL
* ); * );
* *
* CREATE TABLE access * CREATE TABLE access
@ -80,7 +83,7 @@
//// Functions: //// //// Functions: ////
// Adds a post to the database // Adds a post to the database
function post($name, $email, $comment, $img, $save_cookie, $captcha, $captcha_answer) { function post($name, $email, $comment, $img, $spoiler, $save_cookie, $captcha, $captcha_answer) {
$extensions = [ $extensions = [
'image/bmp' => '.bmp', 'image/bmp' => '.bmp',
'image/gif' => '.gif', 'image/gif' => '.gif',
@ -131,14 +134,18 @@
return 'Unable to upload image, please contact the webmaster.'; return 'Unable to upload image, please contact the webmaster.';
} }
// Create thumbnail if(!$spoiler) {
exec("ffmpeg -y -loglevel error -i '$target' -filter_complex 'color=#ffccdd[c];[c][0]scale2ref[cs][0s];[cs][0s]overlay=shortest=1[o];[o]scale=min(200\,iw):-1[o];[o]scale=-1:min(100\,ih)' -frames:v 1 " . THUMB_PATH . "$img_name.jpg", $output, $ret); $thumb_size = getimagesize(ASSETS_PATH . "spoiler.gif");
if($ret != 0) { } else {
@unlink($target); // Create thumbnail
return 'Unable to create thumbnail, please contact the webmaster.'; exec("ffmpeg -y -loglevel error -i '$target' -filter_complex 'color=#ffccdd[c];[c][0]scale2ref[cs][0s];[cs][0s]overlay=shortest=1[o];[o]scale=min(200\,iw):-1[o];[o]scale=-1:min(100\,ih)' -frames:v 1 " . THUMB_PATH . "$img_name.jpg", $output, $ret);
if($ret != 0) {
@unlink($target);
return 'Unable to create thumbnail, please contact the webmaster.';
}
$thumb_size = getimagesize(THUMB_PATH . "$img_name.jpg");
} }
$thumb_size = getimagesize(THUMB_PATH . "$img_name.jpg");
} else { } else {
// The new image file move failed, so delete the temporary file and return an error // The new image file move failed, so delete the temporary file and return an error
@unlink($img['tmp_name']); @unlink($img['tmp_name']);
@ -172,8 +179,8 @@
$trip_name = trip_name($name); $trip_name = trip_name($name);
// Add post to database // Add post to database
$query = 'INSERT INTO posts (user_id, name, trip, email, comment, img, thumb_width, thumb_height) ' . $query = 'INSERT INTO posts (user_id, name, trip, email, comment, img, thumb_width, thumb_height, spoiler) ' .
'VALUES ($1, $2, $3, $4, $5, $6, $7, $8)'; 'VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)';
$params = [ $params = [
empty($uid) ? NULL : $uid, empty($uid) ? NULL : $uid,
empty($trip_name['name']) ? 'Anonymous' : htmlspecialchars($trip_name['name']), empty($trip_name['name']) ? 'Anonymous' : htmlspecialchars($trip_name['name']),
@ -182,7 +189,8 @@
empty($comment) ? NULL : htmlspecialchars($comment), empty($comment) ? NULL : htmlspecialchars($comment),
empty($target) ? NULL : basename($target), empty($target) ? NULL : basename($target),
$thumb_size[0], $thumb_size[0],
$thumb_size[1] $thumb_size[1],
$spoiler ? 'T' : 'F'
]; ];
pg_query_params($query, $params) or die('Query failed: ' . pg_last_error()); pg_query_params($query, $params) or die('Query failed: ' . pg_last_error());
@ -203,7 +211,7 @@
// Prints the post list // Prints the post list
function show_posts() { function show_posts() {
$query = 'SELECT post_id, user_id, name, trip, email, comment, img, thumb_width, thumb_height, post_time FROM posts ORDER BY posts.post_time'; $query = 'SELECT post_id, user_id, name, trip, email, comment, img, thumb_width, thumb_height, spoiler, post_time FROM posts ORDER BY posts.post_time';
$result = pg_query($query) or die('Query failed: ' . pg_last_error()); $result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Clean up old posts // Clean up old posts
@ -257,7 +265,8 @@
echo '<div class="postbody clearfix">'; echo '<div class="postbody clearfix">';
if(!empty($row['img'])){ if(!empty($row['img'])){
echo '<a href="' . UPLOAD_PATH . $row['img'] . '" target="_blank">'; echo '<a href="' . UPLOAD_PATH . $row['img'] . '" target="_blank">';
echo '<img src="' . THUMB_PATH . preg_replace('/\.(?:png|gif|bmp)$/', '.jpg', $row['img']) . "\" alt=\"{$row['img']}\" width=\"{$row['thumb_width']}\" height=\"{$row['thumb_height']}\" />"; $thumb_path = ($row['spoiler'] == 't') ? (ASSETS_PATH . 'spoiler.gif') : (THUMB_PATH . preg_replace('/\.(?:png|gif|bmp)$/', '.jpg', $row['img']));
echo "<img src=\"$thumb_path\" alt=\"{$row['img']}\" width=\"{$row['thumb_width']}\" height=\"{$row['thumb_height']}\" />";
echo '</a>'; echo '</a>';
} }
@ -383,11 +392,12 @@
$email = trim($_POST['email']); $email = trim($_POST['email']);
$comment = trim($_POST['comment']); $comment = trim($_POST['comment']);
$img = $_FILES['img']; $img = $_FILES['img'];
$spoiler = isset($_POST['spoiler']) && !empty($_FILES['img']['tmp_name']);
$save_cookie = isset($_POST['save_cookie']); $save_cookie = isset($_POST['save_cookie']);
$captcha = trim($_POST['captcha']); $captcha = trim($_POST['captcha']);
$captcha_answer = trim($_POST['captcha_answer']); $captcha_answer = trim($_POST['captcha_answer']);
$err = post($name, $email, $comment, $img, $save_cookie, $captcha, $captcha_answer); $err = post($name, $email, $comment, $img, $spoiler, $save_cookie, $captcha, $captcha_answer);
if($err == "") { if($err == "") {
# Redirect to latest post # Redirect to latest post
$query = 'SELECT post_id FROM posts ORDER BY posts.post_time DESC LIMIT 1'; $query = 'SELECT post_id FROM posts ORDER BY posts.post_time DESC LIMIT 1';
@ -480,6 +490,11 @@
<br /> <br />
<br /> <br />
<label for="spoiler">Spoiler:</label>
<input type="checkbox" id="spoiler" name="spoiler" />
<br />
<br />
<label for="captcha">CAPTCHA:</label></td> <label for="captcha">CAPTCHA:</label></td>
<br /> <br />
<code><?php echo $captcha_str; ?></code> <code><?php echo $captcha_str; ?></code>
@ -514,6 +529,10 @@
<td><label for="img">Image:</label></td> <td><label for="img">Image:</label></td>
<td><input type="file" id="img" name="img" /> (Limit: <?php echo MAX_FILE_SIZE >> 10; ?> KiB)</td> <td><input type="file" id="img" name="img" /> (Limit: <?php echo MAX_FILE_SIZE >> 10; ?> KiB)</td>
</tr> </tr>
<tr>
<td><label for="spoiler">Spoiler:</label></td>
<td><input type="checkbox" id="spoiler" name="spoiler" /></td>
</tr>
<tr> <tr>
<td><label for="captcha">CAPTCHA:</label></td> <td><label for="captcha">CAPTCHA:</label></td>
<td> <td>

@ -52,7 +52,7 @@
// Get posts // Get posts
$posts = []; $posts = [];
$query = "SELECT post_id, name, trip, email, comment, img, TO_CHAR(post_time, 'Dy, DD Mon YYYY HH24:MI:SS TZHTZM') AS ts " $query = "SELECT post_id, name, trip, email, comment, img, spoiler, TO_CHAR(post_time, 'Dy, DD Mon YYYY HH24:MI:SS TZHTZM') AS ts "
. "FROM posts ORDER BY posts.post_time DESC LIMIT 10"; . "FROM posts ORDER BY posts.post_time DESC LIMIT 10";
$result = pg_query($query) or die('Query failed: ' . pg_last_error()); $result = pg_query($query) or die('Query failed: ' . pg_last_error());
@ -71,7 +71,7 @@
if(!empty($row['trip'])) if(!empty($row['trip']))
$name .= '◆' . $row['trip']; $name .= '◆' . $row['trip'];
$comment = htmlspecialchars("<p>$comment</p>"); $comment = htmlspecialchars("<p>$comment</p>");
$img = preg_replace('/\.(?:png|gif|bmp)$/', '.jpg', $row['img']); $img = ($row['spoiler'] == 't') ? (ASSETS_PATH . 'spoiler.gif') : (THUMB_PATH . preg_replace('/\.(?:png|gif|bmp)$/', '.jpg', $row['img']));
$posts[] = [ $posts[] = [
'title' => "New post by $name", 'title' => "New post by $name",
@ -81,9 +81,9 @@
'pubDate' => $row['ts'], 'pubDate' => $row['ts'],
'guid' => 'p' . $row['post_id'], 'guid' => 'p' . $row['post_id'],
'img' => empty($row['img']) ? NULL : [ 'img' => empty($row['img']) ? NULL : [
'url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/' . THUMB_PATH . $img, 'url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/' . $img,
'length' => filesize(THUMB_PATH . $img), 'length' => filesize($img),
'type' => mime_content_type(THUMB_PATH . $img) 'type' => mime_content_type($img)
] ]
]; ];
} }

Loading…
Cancel
Save