Store thumbnails based on the hash of original image

This commit is contained in:
Sheldon Lee 2023-11-18 15:56:05 +08:00
parent a6a4d654d4
commit 737821fa6a

View File

@ -65,11 +65,14 @@ function create_thumbnail(string $category, string $file): string | null {
$path = add_seperator_ab("/", $category, $file); $path = add_seperator_ab("/", $category, $file);
$original_file = ROOT_DIR.IMG_DIR.$path; $original_file = ROOT_DIR.IMG_DIR.$path;
$thumbnail_directory = ROOT_DIR.THUMBNAIL_DIR.IMG_DIR.add_seperator_ab("/", $category, ""); $hash = hash_file("sha1", $original_file);
$thumbnail_file = $thumbnail_directory.$file;
$thumbnail_directory = ROOT_DIR.THUMBNAIL_DIR;
$thumbnail_name = "$hash.jpg";
$thumbnail_file = $thumbnail_directory.$thumbnail_name;
if (file_exists($thumbnail_file)) { if (file_exists($thumbnail_file)) {
log_print("$thumbnail_file exists, skipping generation"); log_print("thumbnail for $original_file exists, skipping generation");
goto return_thumbnail; goto return_thumbnail;
} }
@ -80,16 +83,16 @@ function create_thumbnail(string $category, string $file): string | null {
log_error($e->getMessage()); log_error($e->getMessage());
} }
log_print(THUMBNAIL_SCRIPT." '$original_file' '$thumbnail_file'"); log_print("generating thumbnail for $original_file");
$output = shell_exec(THUMBNAIL_SCRIPT." '$original_file' '$thumbnail_file' 2>&1"); $output = shell_exec(THUMBNAIL_SCRIPT." '$original_file' '$thumbnail_file' 2>&1");
log_print($output); if ($output) log_print($output);
if ($output === false) { if ($output === false) {
log_error("Thumbnail failed"); log_error("Thumbnail failed");
return ROOT_URL.IMG_DIR.$path; return ROOT_URL.IMG_DIR.$path;
} }
return_thumbnail: return_thumbnail:
return ROOT_URL.THUMBNAIL_DIR.IMG_DIR.$path; return ROOT_URL.THUMBNAIL_DIR.$thumbnail_name;
} }
function add_seperator_ab(string $separator, string $a, string $b): string { function add_seperator_ab(string $separator, string $a, string $b): string {