Massively faster by skipping many image gen checks; ~0.35s to ~0.0015s

This commit is contained in:
John Alanbrook 2022-11-09 18:00:32 +00:00
parent d3eb3720f7
commit de3c08baa7

View file

@ -9,12 +9,12 @@ if (page_locked($tryuri)) {
load_page("404"); load_page("404");
} }
if (file_exists("site.json")) { $site = file_get_contents("site.json");
$site = json_decode(file_get_contents("site.json"), true); if (!$site) {
} else {
echo "Need a site.json file to work!"; echo "Need a site.json file to work!";
return false; return false;
} }
$site = json_decode($site, true);
if (preg_match('/\.(?:jpg|png|webp|css|ico|js|ttf|woff|txt)/', $_SERVER["REQUEST_URI"])) { if (preg_match('/\.(?:jpg|png|webp|css|ico|js|ttf|woff|txt)/', $_SERVER["REQUEST_URI"])) {
return false; return false;
@ -39,7 +39,7 @@ function get_path_content($path) {
} }
} else { } else {
$fy = "$f/index.json"; $fy = "$f/index.json";
if (file_exists($fy) && !page_locked($fy)) $titles[] = ["c" => json_decode(file_get_contents($fy), true), "link" => uri_from_file($fy)]; if (is_file($fy) && !page_locked($fy)) $titles[] = ["c" => json_decode(file_get_contents($fy), true), "link" => uri_from_file($fy)];
} }
} }
@ -70,22 +70,22 @@ $parsef = "";
$tryf = "$tryuri/index"; $tryf = "$tryuri/index";
$tryf = ltrim($tryf, '/'); $tryf = ltrim($tryf, '/');
if (file_exists("$tryf.json")) { if (is_file("$tryf.json")) {
$jsonf = "$tryf.json"; $jsonf = "$tryf.json";
} else if (file_exists("$tryuri.json")) { } else if (is_file("$tryuri.json")) {
$jsonf = "$tryuri.json"; $jsonf = "$tryuri.json";
} }
if (file_exists("$tryf.php")) { if (is_file("$tryf.php")) {
$mainphp = "$tryf.php"; $mainphp = "$tryf.php";
} }
else if (file_exists("$tryuri.php")) { else if (is_file("$tryuri.php")) {
$mainphp = "$tryuri.php"; $mainphp = "$tryuri.php";
} }
if (file_exists("$tryf.md")) { if (is_file("$tryf.md")) {
$parsef = "$tryf.md"; $parsef = "$tryf.md";
} else if (file_exists("$tryuri.md")) { } else if (is_file("$tryuri.md")) {
$parsef = "$tryuri.md"; $parsef = "$tryuri.md";
} }
@ -97,7 +97,7 @@ if (empty($mainphp) && empty($jsonf) && empty($parsef)) {
if (empty($mainphp)) { if (empty($mainphp)) {
while ($tryuri != "") { while ($tryuri != "") {
if (file_exists("$tryuri/temp.php")) { if (is_file("$tryuri/temp.php")) {
$mainphp = "$tryuri/temp.php"; $mainphp = "$tryuri/temp.php";
goto endmain; goto endmain;
} }
@ -150,7 +150,7 @@ return $write;
} }
function page_locked($page) { function page_locked($page) {
if (file_exists("$page.lock" )) if (is_file("$page.lock" ))
return true; return true;
while ($page != "" && is_dir($page)) { while ($page != "" && is_dir($page)) {
@ -174,52 +174,53 @@ function page_locked($page) {
function p_img($src, $opts, $ext) { function p_img($src, $opts, $ext) {
$src = ltrim($src, '/'); $src = ltrim($src, '/');
$p = pathinfo($src); $p = substr($src, 0, strrpos($src, '.'));
$o = str_replace([" ", "-"], "", $opts); $o = str_replace([" ", "-"], "", $opts);
if ($o == "") $o = "gen"; if ($o == "") $o = "gen";
return "/gen/$p[dirname]/$p[filename].$o.$ext"; return "/gen/$p.$o.$ext";
}
function sub_ext($src, $ext) {
$stem = substr($src, 0, strrpos($src, '.'));
return "$stem.$ext";
} }
/* Given a source image and options, creates the appropriate images /* Given a source image and options, creates the appropriate images
* jpeg, webp, and avif for lossy, and png and lossless webp for lossless * jpeg, webp, and avif for lossy, and png and lossless webp for lossless
*/ */
function gen_images($src, $opts = "", $lossy=0) { function gen_images($src, $opts = "", $lossy=0) {
$psrc = ltrim($src, '/'); $ext = substr($src, strrpos($src, '.')+1);
$path = pathinfo($psrc);
if ($lossy || in_array($path['extension'], ["jpg", "jpeg"])) { if ($lossy || in_array($ext, ["jpg", "jpeg"])) {
$je = ltrim(p_img($src, $opts, "jpg"), '/'); $je = ltrim(p_img($src, $opts, "jpg"), '/');
$jpath = pathinfo($je); if (is_file($je)) return;
$genp = $jpath['dirname'];
`mkdir -p $genp`; $gpath = substr($je, 0, strrpos($je, '/'));
`mkdir -p $gpath`;
`convert -quality 80 $opts $psrc $je`; `convert -quality 80 $opts $psrc $je`;
$we = ltrim(p_img($src, $opts, "webp"), '/'); $we = sub_ext($je, "webp");
if (!file_exists($we)) {
`convert -quality 80 $opts $psrc $we`; `convert -quality 80 $opts $psrc $we`;
}
$ae = ltrim(p_img($src, $opts, "avif"), '/'); $ae = sub_ext($je, "avif");
if (!file_exists($ae)) {
`convert -quality 80 $opts $psrc $ae`; `convert -quality 80 $opts $psrc $ae`;
}
return; return;
} }
if ($path['extension'] == "png") { if ($ext == "png") {
$we = ltrim(p_img($src, $opts, "webp"), '/'); $we = ltrim(p_img($src, $opts, "webp"), '/');
$wpath = pathinfo($we); if (is_file($we)) return;
$genp = $wpath['dirname'];
`mkdir -p $genp`; $gpath = substr($we, 0, strrpos($we, '/'));
`mkdir -p $gpath`;
if (!file_exists($we)) {
`convert -define webp:lossless=true $opts $psrc $we`; `convert -define webp:lossless=true $opts $psrc $we`;
}
return; return;
} }