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),
img VARCHAR(256),
thumb_width INT,
thumb_height INT
thumb_height INT,
spoiler BOOL
);
```
```sql

@ -61,7 +61,10 @@
* trip VARCHAR(10),
* email VARCHAR(256),
* comment VARCHAR(2048),
* img VARCHAR(256)
* img VARCHAR(256),
* thumb_width INT,
* thumb_height INT,
* spoiler BOOL
* );
*
* CREATE TABLE access
@ -80,7 +83,7 @@
//// Functions: ////
// 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 = [
'image/bmp' => '.bmp',
'image/gif' => '.gif',
@ -131,14 +134,18 @@
return 'Unable to upload image, please contact the webmaster.';
}
// Create thumbnail
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.';
if(!$spoiler) {
$thumb_size = getimagesize(ASSETS_PATH . "spoiler.gif");
} else {
// Create thumbnail
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 {
// The new image file move failed, so delete the temporary file and return an error
@unlink($img['tmp_name']);
@ -172,8 +179,8 @@
$trip_name = trip_name($name);
// Add post to database
$query = 'INSERT INTO posts (user_id, name, trip, email, comment, img, thumb_width, thumb_height) ' .
'VALUES ($1, $2, $3, $4, $5, $6, $7, $8)';
$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, $9)';
$params = [
empty($uid) ? NULL : $uid,
empty($trip_name['name']) ? 'Anonymous' : htmlspecialchars($trip_name['name']),
@ -182,7 +189,8 @@
empty($comment) ? NULL : htmlspecialchars($comment),
empty($target) ? NULL : basename($target),
$thumb_size[0],
$thumb_size[1]
$thumb_size[1],
$spoiler ? 'T' : 'F'
];
pg_query_params($query, $params) or die('Query failed: ' . pg_last_error());
@ -203,7 +211,7 @@
// Prints the post list
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());
// Clean up old posts
@ -257,7 +265,8 @@
echo '<div class="postbody clearfix">';
if(!empty($row['img'])){
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>';
}
@ -383,11 +392,12 @@
$email = trim($_POST['email']);
$comment = trim($_POST['comment']);
$img = $_FILES['img'];
$spoiler = isset($_POST['spoiler']) && !empty($_FILES['img']['tmp_name']);
$save_cookie = isset($_POST['save_cookie']);
$captcha = trim($_POST['captcha']);
$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 == "") {
# Redirect to latest post
$query = 'SELECT post_id FROM posts ORDER BY posts.post_time DESC LIMIT 1';
@ -480,6 +490,11 @@
<br />
<br />
<label for="spoiler">Spoiler:</label>
<input type="checkbox" id="spoiler" name="spoiler" />
<br />
<br />
<label for="captcha">CAPTCHA:</label></td>
<br />
<code><?php echo $captcha_str; ?></code>
@ -514,6 +529,10 @@
<td><label for="img">Image:</label></td>
<td><input type="file" id="img" name="img" /> (Limit: <?php echo MAX_FILE_SIZE >> 10; ?> KiB)</td>
</tr>
<tr>
<td><label for="spoiler">Spoiler:</label></td>
<td><input type="checkbox" id="spoiler" name="spoiler" /></td>
</tr>
<tr>
<td><label for="captcha">CAPTCHA:</label></td>
<td>

@ -52,7 +52,7 @@
// Get 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";
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
@ -71,7 +71,7 @@
if(!empty($row['trip']))
$name .= '◆' . $row['trip'];
$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[] = [
'title' => "New post by $name",
@ -81,9 +81,9 @@
'pubDate' => $row['ts'],
'guid' => 'p' . $row['post_id'],
'img' => empty($row['img']) ? NULL : [
'url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/' . THUMB_PATH . $img,
'length' => filesize(THUMB_PATH . $img),
'type' => mime_content_type(THUMB_PATH . $img)
'url' => $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/' . $img,
'length' => filesize($img),
'type' => mime_content_type($img)
]
];
}

Loading…
Cancel
Save