diff options
| author | ilotterytea <iltsu@alright.party> | 2025-05-04 17:08:41 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-05-04 17:08:41 +0500 |
| commit | d20211fad9bc61b9e92c70707ecbe2a5a30fe75a (patch) | |
| tree | 268bd063c01a97d8d2e04826b7035e79dac06f72 | |
| parent | c01fd37880b595fe7cc6c099373d3123f015d812 (diff) | |
upd: use imagemagick cli for resizing images
| -rw-r--r-- | public/emotes/upload.php | 45 | ||||
| -rw-r--r-- | src/images.php | 84 |
2 files changed, 34 insertions, 95 deletions
diff --git a/public/emotes/upload.php b/public/emotes/upload.php index 89abf44..de4d2d4 100644 --- a/public/emotes/upload.php +++ b/public/emotes/upload.php @@ -23,7 +23,7 @@ if (isset($_SESSION["user_role"]) && $_SESSION["user_role"]["permission_upload"] $uploader_name = $_SESSION["user_name"] ?? ANONYMOUS_DEFAULT_NAME; } -function abort_upload(string $path, PDO $db, string $id, string $response_text, int $response_code = 400) +function abort_upload(string $path, PDO $db, string $id) { $stmt = $db->prepare("DELETE FROM emotes WHERE id = ?"); $stmt->execute([$id]); @@ -31,8 +31,6 @@ function abort_upload(string $path, PDO $db, string $id, string $response_text, array_map("unlink", glob("$path/*.*")); rmdir($path); - http_response_code($response_code); - exit($response_text); } include "../../src/utils.php"; @@ -59,6 +57,7 @@ if ($_SERVER['REQUEST_METHOD'] != "POST") { <?php html_navigation_bar() ?> <section class="content" style="width: 50%;"> + <?php display_alert() ?> <section class="box"> <div class="box navtab"> <div> @@ -215,16 +214,6 @@ if ($code == "" || !preg_match(EMOTE_NAME_REGEX, $code)) { $image = $_FILES["file"]; -if (is_null(list($mime, $ext) = get_mime_and_ext($image["tmp_name"]))) { - http_response_code(400); - echo json_encode([ - "status_code" => 400, - "message" => "Not a valid image", - "data" => null - ]); - exit; -} - $notes = str_safe($_POST["notes"] ?? "", EMOTE_COMMENT_MAX_LENGTH); if (empty($notes)) { $notes = null; @@ -250,23 +239,23 @@ if (!is_dir($path)) { } // resizing the image - -// 3x image -$resized_image = resize_image($image["tmp_name"], "$path/3x", $max_width, $max_height); -if ($resized_image) { - abort_upload($path, $db, $id, $resized_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; } - -// 2x image -$resized_image = resize_image($image["tmp_name"], "$path/2x", $max_width / 2, $max_height / 2); -if ($resized_image) { - abort_upload($path, $db, $id, $resized_image); +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; } - -// 1x image -$resized_image = resize_image($image["tmp_name"], "$path/1x", $max_width / 4, $max_height / 4); -if ($resized_image) { - abort_upload($path, $db, $id, $resized_image); +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; diff --git a/src/images.php b/src/images.php index 6ce8b5a..9a2c2a5 100644 --- a/src/images.php +++ b/src/images.php @@ -1,80 +1,30 @@ <?php -function resize_image(string $src_path, string $dst_path, int $max_width, int $max_height, bool $set_format = true, bool $stretch = false): string|null +function resize_image(string $src_path, string $dst_path, int $max_width, int $max_height, bool $set_format = true, bool $stretch = false): int|null { - if ($src_path == "" || !getimagesize($src_path)) { - return json_encode([ - "status_code" => 400, - "message" => "Not an image", - "data" => null - ]); - } - - $imagick = new Imagick(); - - $imagick->readImage($src_path); - $format = ".webp"; - - if (!$set_format) { - $format = ""; + if ($src_path == "") { + return -2; } - if ($imagick->getNumberImages() > 1) { - $imagick = $imagick->coalesceImages(); - - foreach ($imagick as $frame) { - $width = $frame->getImageWidth(); - $height = $frame->getImageHeight(); - $ratio = min($max_width / $width, $max_height / $height); - $new_width = (int) ($width * $ratio); - $new_height = (int) ($height * $ratio); - - if ($stretch) { - $new_width = $max_width; - $new_height = $max_height; - } + $image = getimagesize($src_path); - $frame->resizeImage($new_width, $new_height, Imagick::FILTER_TRIANGLE, 1); - $frame->setImagePage($new_width, $new_height, 0, 0); - } - - $imagick = $imagick->deconstructImages(); - $imagick->writeImages("$dst_path$format", true); - } else { - $width = $imagick->getImageWidth(); - $height = $imagick->getImageHeight(); - $ratio = min($max_width / $width, $max_height / $height); - $new_width = (int) ($width * $ratio); - $new_height = (int) ($height * $ratio); - - if ($stretch) { - $new_width = $max_width; - $new_height = $max_height; - } - - $imagick->resizeImage($new_width, $new_height, Imagick::FILTER_TRIANGLE, 1); - $imagick->writeImage("$dst_path$format"); + if ($image == false) { + return -1; } - $imagick->clear(); - $imagick->destroy(); - - return null; -} + $format = $set_format ? ".webp" : ""; -function get_mime_and_ext(string $src_path): array|null -{ - if ($src_path == "") { - return null; - } + $width = $image[0]; + $height = $image[1]; + $ratio = min($max_width / $width, $max_height / $height); + $new_width = $stretch ? $max_width : (int) ($width * $ratio); + $new_height = $stretch ? $max_height : (int) ($height * $ratio); - $imagick = new Imagick(); + $input_path = escapeshellarg($src_path); + $output_path = escapeshellarg("$dst_path$format"); - $imagick->readImage($src_path); - $ext = strtolower($imagick->getImageFormat()); - $mime = $imagick->getImageMimeType(); + $result_code = null; - $imagick->clear(); - $imagick->destroy(); + exec(command: "magick convert $input_path -coalesce -resize {$new_width}x$new_height -layers optimize -loop 0 $output_path", result_code: $result_code); - return [$mime, $ext]; + return $result_code; }
\ No newline at end of file |
