From d7973f5dd033359b77015b67b7a81c595b3180e4 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 7 May 2025 03:20:20 +0500 Subject: feat: manual emote resize --- public/emotes/upload.php | 245 +++++++++++++++++++++++++++++++++++------------ public/static/style.css | 4 + src/images.php | 10 ++ 3 files changed, 197 insertions(+), 62 deletions(-) diff --git a/public/emotes/upload.php b/public/emotes/upload.php index b5a11aa..0b79b5c 100644 --- a/public/emotes/upload.php +++ b/public/emotes/upload.php @@ -67,10 +67,29 @@ if ($_SERVER['REQUEST_METHOD'] != "POST") {
+

Image*

+ + + + + +
+ + +
+

Emote name*

-

Image*

-
@@ -96,7 +115,7 @@ if ($_SERVER['REQUEST_METHOD'] != "POST") {
-
@@ -121,12 +141,18 @@ if ($_SERVER['REQUEST_METHOD'] != "POST") { @@ -197,30 +299,25 @@ if ($_SERVER['REQUEST_METHOD'] != "POST") { exit; } -if (!isset($_FILES["file"])) { - http_response_code(400); - echo json_encode([ - "status_code" => 400, - "message" => "No file set", - "data" => null - ]); +$is_manual = intval($_POST["manual"] ?? "0") == 1; + +if ($is_manual && !isset($_FILES["file-1x"], $_FILES["file-2x"], $_FILES["file-3x"])) { + generate_alert("/emotes/upload.php", "No files set"); + exit; +} + +if (!$is_manual && !isset($_FILES["file"])) { + generate_alert("/emotes/upload.php", "No file set"); exit; } $code = str_safe($_POST["code"] ?? "", EMOTE_NAME_MAX_LENGTH); if ($code == "" || !preg_match(EMOTE_NAME_REGEX, $code)) { - http_response_code(400); - echo json_encode([ - "status_code" => 400, - "message" => "Invalid code", - "data" => null - ]); + generate_alert("/emotes/upload.php", "Invalid code"); exit; } -$image = $_FILES["file"]; - $notes = str_safe($_POST["notes"] ?? "", EMOTE_COMMENT_MAX_LENGTH); if (empty($notes)) { $notes = null; @@ -245,41 +342,65 @@ if (!is_dir($path)) { mkdir($path, 0777, true); } -// resizing the image -if ($err = resize_image($image["tmp_name"], "$path/3x", $max_width, $max_height)) { - error_log("Error processing image: $err"); - generate_alert("/emotes/upload.php", "Error occurred while processing the image ($err)", 500); - abort_upload($path, $db, $id); - exit; -} -if ($err = resize_image($image["tmp_name"], "$path/2x", $max_width / 2, $max_height / 2)) { - error_log("Error processing image: $err"); - generate_alert("/emotes/upload.php", "Error occurred while processing the image ($err)", 500); - abort_upload($path, $db, $id); - exit; -} -if ($err = resize_image($image["tmp_name"], "$path/1x", $max_width / 4, $max_height / 4)) { - error_log("Error processing image: $err"); - generate_alert("/emotes/upload.php", "Error occurred while processing the image ($err)", 500); - abort_upload($path, $db, $id); - exit; +if ($is_manual) { + $image_1x = $_FILES["file-1x"]; + $image_2x = $_FILES["file-2x"]; + $image_3x = $_FILES["file-3x"]; + + $file_1x = does_file_meet_requirements($image_1x["tmp_name"], $max_width / 4, $max_height / 4); + $file_2x = does_file_meet_requirements($image_2x["tmp_name"], $max_width / 2, $max_height / 2); + $file_3x = does_file_meet_requirements($image_3x["tmp_name"], $max_width, $max_height); + + if (!$file_1x[0] || !$file_2x[0] || !$file_3x[0]) { + generate_alert("/emotes/upload.php", "Files don't meet requirements"); + abort_upload($path, $db, $id); + exit; + } + + if ( + !move_uploaded_file($image_1x["tmp_name"], "$path/1x.$file_1x[1]") || + !move_uploaded_file($image_2x["tmp_name"], "$path/2x.$file_2x[1]") || + !move_uploaded_file($image_3x["tmp_name"], "$path/3x.$file_3x[1]") + ) { + generate_alert("/emotes/upload.php", "Failed to move the uploaded files"); + abort_upload($path, $db, $id); + exit; + } +} else { + $image = $_FILES["file"]; + // resizing the image + if ($err = resize_image($image["tmp_name"], "$path/3x", $max_width, $max_height)) { + error_log("Error processing image: $err"); + generate_alert("/emotes/upload.php", "Error occurred while processing the image ($err)", 500); + abort_upload($path, $db, $id); + exit; + } + if ($err = resize_image($image["tmp_name"], "$path/2x", $max_width / 2, $max_height / 2)) { + error_log("Error processing image: $err"); + generate_alert("/emotes/upload.php", "Error occurred while processing the image ($err)", 500); + abort_upload($path, $db, $id); + exit; + } + if ($err = resize_image($image["tmp_name"], "$path/1x", $max_width / 4, $max_height / 4)) { + error_log("Error processing image: $err"); + generate_alert("/emotes/upload.php", "Error occurred while processing the image ($err)", 500); + abort_upload($path, $db, $id); + exit; + } } $db = null; if (CLIENT_REQUIRES_JSON) { - http_response_code(201); - echo json_encode([ + json_response([ "status_code" => 201, "message" => null, "data" => [ "id" => $id, "code" => $code, - "ext" => $ext, - "mime" => $mime, "uploaded_by" => $uploaded_by ] - ]); + ], 201); exit; } diff --git a/public/static/style.css b/public/static/style.css index b658488..f39fc2c 100644 --- a/public/static/style.css +++ b/public/static/style.css @@ -67,6 +67,10 @@ input { border: 1px solid gray; } +input[type=file] { + max-width: 230px; +} + form { display: flex; flex-direction: column; diff --git a/src/images.php b/src/images.php index 9a2c2a5..88db094 100644 --- a/src/images.php +++ b/src/images.php @@ -27,4 +27,14 @@ function resize_image(string $src_path, string $dst_path, int $max_width, int $m exec(command: "magick convert $input_path -coalesce -resize {$new_width}x$new_height -layers optimize -loop 0 $output_path", result_code: $result_code); return $result_code; +} + +function does_file_meet_requirements(string $path, int $max_width, int $max_height): array +{ + $file = getimagesize($path); + if (!$file) { + return [false, null]; + } + + return [$file[0] <= $max_width && $file[1] <= $max_height, image_type_to_extension(intval($file[2]), false)]; } \ No newline at end of file -- cgit v1.2.3