Slight refactor and add stub function to create smaller thumbnail images

This commit is contained in:
Sheldon Lee 2023-11-12 23:18:32 +08:00
parent f644890e3a
commit 76e47a033f

View File

@ -1,7 +1,10 @@
<?php <?php
include_once __DIR__ . "/logging.php";
define("ROOT_URL", "http://localhost/"); define("ROOT_URL", "http://localhost/");
define("IMG_DIR", "/var/www/localhost/img/"); define("ROOT_DIR", "/var/www/localhost/");
define("IMG_URL", "img/"); define("IMG_DIR", "img/");
define("THUMBNAIL_DIR", __DIR__ . "../thumbnails");
main(); main();
@ -10,24 +13,32 @@ function main(): void {
$array = []; $array = [];
$category = $data["category"]; $category = $data["category"];
$directory = IMG_DIR.$category; $directory = ROOT_DIR.IMG_DIR.$category;
if (!is_dir($directory)) return;
if (!is_dir($directory)) goto encode;
foreach (scandir($directory) as $file) { foreach (scandir($directory) as $file) {
if ($file === ".") continue; if ($file === ".") continue;
if ($category === "" && $file === "..") continue; if ($category === "" && $file === "..") continue;
if ($category != "") $file = $category.'/'.$file; if ($category != "") $file = $category.'/'.$file;
$is_dir = is_dir(IMG_DIR.$file); $is_dir = is_dir(ROOT_DIR.IMG_DIR.$file);
if (!$is_dir && !is_valid_file_type($file)) continue; if (!$is_dir && !is_valid_file_type($file)) continue;
$file_item = []; $file_item = [];
$file_item["is_dir"] = $is_dir; $file_item["is_dir"] = $is_dir;
$file_item["url"] = ROOT_URL.IMG_URL.$file; $file_item["url"] = ROOT_URL.IMG_DIR.$file;
$file_item["thumbnail_url"] = create_thumbnail($file);
array_push($array, $file_item); array_push($array, $file_item);
} }
encode:
echo json_encode($array); echo json_encode($array);
} }
function create_thumbnail(string $file): string {
return ROOT_URL.IMG_DIR.$file;
}
function is_valid_file_type(string $filename): bool { function is_valid_file_type(string $filename): bool {
$file_extensions = [ $file_extensions = [
// Image extensions // Image extensions