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