summaryrefslogtreecommitdiff
path: root/src/images.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/images.php')
-rw-r--r--src/images.php84
1 files changed, 17 insertions, 67 deletions
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