summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/emotes/upload.php17
-rw-r--r--src/images.php27
2 files changed, 29 insertions, 15 deletions
diff --git a/public/emotes/upload.php b/public/emotes/upload.php
index 5fd2faa..2c37c26 100644
--- a/public/emotes/upload.php
+++ b/public/emotes/upload.php
@@ -369,21 +369,8 @@ if ($is_manual) {
} 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);
+ if ($err = create_image_bundle($image["tmp_name"], $path, $max_width, $max_height)) {
+ generate_alert("/emotes/upload.php", "Error occurred while processing images ($err)", 500);
abort_upload($path, $db, $id);
exit;
}
diff --git a/src/images.php b/src/images.php
index 837ebaf..9462641 100644
--- a/src/images.php
+++ b/src/images.php
@@ -29,6 +29,33 @@ function resize_image(string $src_path, string $dst_path, int $max_width, int $m
return $result_code;
}
+function create_image_bundle(string $src_path, string $dst_path, int $max_width, int $max_height, bool $set_format = true, bool $stretch = false): int|null
+{
+ if (!is_dir($dst_path)) {
+ mkdir($dst_path, 0777, true);
+ }
+
+ if ($err = resize_image($src_path, "$dst_path/3x", $max_width, $max_height, $set_format, $stretch)) {
+ array_map("unlink", glob("$dst_path/*.*"));
+ rmdir($dst_path);
+ return $err;
+ }
+
+ if ($err = resize_image($src_path, "$dst_path/2x", round($max_width / 2), round($max_height / 2), $set_format, $stretch)) {
+ array_map("unlink", glob("$dst_path/*.*"));
+ rmdir($dst_path);
+ return $err;
+ }
+
+ if ($err = resize_image($src_path, "$dst_path/1x", round($max_width / 4), round($max_height / 4), $set_format, $stretch)) {
+ array_map("unlink", glob("$dst_path/*.*"));
+ rmdir($dst_path);
+ return $err;
+ }
+
+ return null;
+}
+
function get_file_extension(string $path): string|null
{
if ($file = getimagesize($path)) {