added new thumbnail generation

This commit is contained in:
Luca Haid
2025-07-10 13:17:19 +02:00
parent 2b0e662190
commit a322f32f88
2 changed files with 52 additions and 10 deletions

View File

@@ -90,23 +90,63 @@ class FileController extends mfBaseController {
protected function showAction() {
$id = $this->request->id;
if (!is_numeric($id) || $id < 1) return true;
$size = $this->request->size;
if (!is_numeric($id) || $id < 1) {
http_response_code(400);
self::returnJson(["error" => "Invalid File ID"]);
return;
}
$file = new File($id);
if (!$file || !$file->id) throw new Exception("File record not found", 404);
if (!$file->id) {
http_response_code(404);
self::returnJson(["error" => "File record not found"]);
return;
}
$path = MFUPLOAD_FILE_SAVE_PATH . ($file->subfolder ? "/{$file->subfolder}" : "") . "/{$file->store_filename}";
$originalPath = MFUPLOAD_FILE_SAVE_PATH . ($file->subfolder ? "/{$file->subfolder}" : "") . "/{$file->store_filename}";
if (!is_readable($originalPath)) {
http_response_code(404);
self::returnJson(["error" => "Physical file not found"]);
return;
}
if (!is_readable($path)) throw new Exception("Physical file not found", 4041);
$imageInfo = @getimagesize($originalPath);
if ($imageInfo === false) {
$this->downloadAction();
return;
}
if (($imageInfo = @getimagesize($path)) !== false) {
$sizeDimensions = ['tiny' => '100x100', 'small' => '250x250', 'medium' => '800x800', 'large' => '1200x1200'];
if (empty($size) || !isset($sizeDimensions[$size])) {
header('Content-Type: ' . $imageInfo['mime']);
header('Content-Disposition: inline; filename="' . ($file->orig_filename ?: $file->store_filename) . '"');
readfile($path);
readfile($originalPath);
exit;
} else {
throw new Exception("File is not a displayable image.", 415);
}
$cacheDir = TEMP_DIR . "/thumbnails";
@mkdir($cacheDir, 0775, true);
$cachedPath = "{$cacheDir}/{$id}_{$size}." . pathinfo($originalPath, PATHINFO_EXTENSION);
if (!file_exists($cachedPath)) {
$command = "convert " . escapeshellarg($originalPath) . " -resize " . escapeshellarg($sizeDimensions[$size]) . " " . escapeshellarg($cachedPath);
exec($command, $output, $return_var);
if ($return_var !== 0) {
http_response_code(500);
self::returnJson(["error" => "Failed to create thumbnail."]);
return;
}
}
header('Content-Type: ' . $imageInfo['mime']);
header('Content-Disposition: inline; filename="' . basename($cachedPath) . '"');
readfile($cachedPath);
exit;
}
}