Files
decpdf.site/app/controllers/CoverController.php
2026-01-18 00:53:18 +00:00

67 lines
1.4 KiB
PHP

<?php
class CoverController {
static function get_cover($_request, $id, $filename, $size = false) {
$rev = new Revision($id);
if ($rev === false) return "";
$path = $rev->path();
$pdf = new PDF($path . "/doc.pdf");
if (!$pdf->exists()) {
return [404, "Not Found"];
}
if (!$size) {
$imgpath = sprintf("%s/cover.jpg", $path);
} else {
$imgpath = sprintf("%s/cover-%d.jpg", $path, $size);
}
$file = new File($imgpath);
if ($file->exists()) {
$h = $file->hash();
$h = "\"$h\"";
if ($h == $_request->header("If-None-Match")) {
return [304, "Not Modified", [
"ETag" => $h,
"Cache-Control" => "public, max-age=86400, must-revalidate",
]];
}
return new Image($file);
}
$cover = new Image($path . "/cover.jpg");
if (!$cover->exists()) {
$pdf->extract_page(0, $cover->path());
$cover = new Image($path . "/cover.jpg");
}
if (!$cover->exists()) {
return [404, "Not Found"];
}
if ($size === false) $size = $cover->width();
if ($size >= $cover->width()) {
$h = $cover->hash();
$h = "\"$h\"";
if ($h == $_request->header("If-None-Match")) {
return [304, "Not Modified", [
"ETag" => $h,
"Cache-Control" => "public, max-age=86400, must-revalidate",
]];
}
return $cover;
}
$cover->scale($size);
$img = $cover->save($imgpath, "image/jpeg");
return $img;
}
}