diff options
| author | ilotterytea <iltsu@alright.party> | 2025-05-12 14:06:00 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-05-12 14:06:00 +0500 |
| commit | 952058eed3a6e8d3eb04dc8795e647ef4f655306 (patch) | |
| tree | 41da58a4c4ae90bf3fde1826cd5983881c993cce /src/images.php | |
| parent | 01918a6fd58590fe213a8e0f449a607915808b3b (diff) | |
feat: function for creating image bundles
Diffstat (limited to 'src/images.php')
| -rw-r--r-- | src/images.php | 27 |
1 files changed, 27 insertions, 0 deletions
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)) { |
