From 9f6302fa48ced1be0c5aaac0498cf48f202e41dc Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Tue, 8 Nov 2022 21:17:39 +0000 Subject: [PATCH] Add generic image conversion property; generates images to a /gen/ folder. --- router.php | 108 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 30 deletions(-) diff --git a/router.php b/router.php index 7a89068..8b0a3c5 100644 --- a/router.php +++ b/router.php @@ -172,49 +172,97 @@ function page_locked($page) { return false; } -function gen_images($src) { - $path = pathinfo($src); - - - - if (in_array($path['extension'], ["jpg", "jpeg"])) { - $pwebp = "$path[dirname]/$path[filename].webp"; - $pe = ltrim($src, '/'); - $we = ltrim($pwebp, '/'); - - if (!file_exists($we)) { - exec("convert -quality 80 $pe $we"); - } - } +function p_img($src, $opts, $ext) { + $src = ltrim($src, '/'); + $p = pathinfo($src); + $o = str_replace([" ", "-"], "", $opts); + if ($o == "") $o = "gen"; + return "/gen/$p[dirname]/$p[filename].$o.$ext"; } -function make_img($src, $q=80) { +/* 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 = "") { + $psrc = ltrim($src, '/'); + $path = pathinfo($psrc); + + if (in_array($path['extension'], ["jpg", "jpeg"])) { + $je = ltrim(p_img($src, $opts, "jpg"), '/'); + + $jpath = pathinfo($je); + $genp = $jpath['dirname']; + `mkdir -p $genp`; + + `convert -quality 80 $opts $psrc $je`; + + $we = ltrim(p_img($src, $opts, "webp"), '/'); + if (!file_exists($we)) { + `convert -quality 80 $opts $psrc $we`; + } + + $ae = ltrim(p_img($src, $opts, "avif"), '/'); + if (!file_exists($ae)) { + `convert -quality 80 $opts $psrc $ae`; + } + } + + + if ($path['extension'] == "png") { + $we = ltrim(p_img($src, $opts, "webp"), '/'); + + $wpath = pathinfo($we); + $genp = $wpath['dirname']; + `mkdir -p $genp`; + + if (!file_exists($we)) { + `convert -quality 80 -define webp:lossless=true $psrc $we`; + } + } + +} + +function make_img($src, $opts="", $q=80) { $p = img_src($src); - gen_images($p); - $path = pathinfo($p); - $pwebp = "$path[dirname]/$path[filename].webp"; + gen_images($p, $opts); + + if (pathinfo($p)['extension'] == "png") { + $pe = $p; + $we = p_img($p, $opts, "webp"); + + echo << + + + + + END; + } else { + + $we = p_img($p, $opts, "webp"); + $je = p_img($p, $opts, "jpg"); echo << - - - + + + - -END; + END; + } } -function make_bkgd_img($src, $q=80) { + + +function make_bkgd_img($src, $opts = "", $q=80) { $p = img_src($src); - gen_images($p); - $path = pathinfo($p); - $pwebp = "$path[dirname]/$path[filename].webp"; + gen_images($p, $opts); - $pe = ltrim($pwebp, '/'); - $we = ltrim($p, '/'); + $we = p_img($p, $opts, "webp"); + $je = p_img($p, $opts, "jpg"); - return "background-image: url($pe); background-image: -webkit-image-set(url($pwebp) 1x, url($p) 1x)"; + return "background-image: url($je); background-image: -webkit-image-set(url($we) 1x, url($je) 1x)"; }