summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-19 16:47:23 +0500
committerilotterytea <iltsu@alright.party>2025-04-19 16:47:23 +0500
commit9c2f42e423096bdf56b7f96e07221f6420b0e337 (patch)
tree5cbcc6054f0727b4777590d6c25f6c66c68b0075 /src
parentad9ed65599cdcbd90fd6862caae23f5141cdfbf7 (diff)
feat: upload emotes
Diffstat (limited to 'src')
-rw-r--r--src/images.php48
-rw-r--r--src/utils.php33
2 files changed, 81 insertions, 0 deletions
diff --git a/src/images.php b/src/images.php
new file mode 100644
index 0000000..0e09814
--- /dev/null
+++ b/src/images.php
@@ -0,0 +1,48 @@
+<?php
+function resize_image(string $src_path, string $dst_path, int $max_width, int $max_height): string|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 = strtolower($imagick->getImageFormat());
+
+ 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);
+
+ $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);
+
+ $imagick->resizeImage($new_width, $new_height, Imagick::FILTER_TRIANGLE, 1);
+ $imagick->writeImage("$dst_path.$format");
+ }
+
+ $imagick->clear();
+ $imagick->destroy();
+
+ return null;
+} \ No newline at end of file
diff --git a/src/utils.php b/src/utils.php
new file mode 100644
index 0000000..59ecad4
--- /dev/null
+++ b/src/utils.php
@@ -0,0 +1,33 @@
+<?php
+function generate_random_string(int $length): string
+{
+ $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+ $output = "";
+
+ for ($i = 0; $i < $length; $i++) {
+ $charindex = random_int(0, strlen($chars) - 1);
+ $output .= $chars[$charindex];
+ }
+
+ return $output;
+}
+
+function str_safe(string $s, int|null $max_length, bool $remove_new_lines = true): string
+{
+ $output = $s;
+
+ if ($remove_new_lines) {
+ $output = str_replace(PHP_EOL, "", $output);
+ }
+
+ $output = htmlspecialchars($output);
+ $output = strip_tags($output);
+
+ if ($max_length) {
+ $output = substr($output, 0, $max_length);
+ }
+
+ $output = trim($output);
+
+ return $output;
+} \ No newline at end of file