Add generic image conversion property; generates images to a /gen/ folder.
This commit is contained in:
parent
3953a4eb2a
commit
9f6302fa48
92
router.php
92
router.php
|
@ -172,49 +172,97 @@ function page_locked($page) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function gen_images($src) {
|
function p_img($src, $opts, $ext) {
|
||||||
$path = pathinfo($src);
|
$src = ltrim($src, '/');
|
||||||
|
$p = pathinfo($src);
|
||||||
|
$o = str_replace([" ", "-"], "", $opts);
|
||||||
|
if ($o == "") $o = "gen";
|
||||||
|
return "/gen/$p[dirname]/$p[filename].$o.$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 = "") {
|
||||||
|
$psrc = ltrim($src, '/');
|
||||||
|
$path = pathinfo($psrc);
|
||||||
|
|
||||||
if (in_array($path['extension'], ["jpg", "jpeg"])) {
|
if (in_array($path['extension'], ["jpg", "jpeg"])) {
|
||||||
$pwebp = "$path[dirname]/$path[filename].webp";
|
$je = ltrim(p_img($src, $opts, "jpg"), '/');
|
||||||
$pe = ltrim($src, '/');
|
|
||||||
$we = ltrim($pwebp, '/');
|
$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)) {
|
if (!file_exists($we)) {
|
||||||
exec("convert -quality 80 $pe $we");
|
`convert -quality 80 -define webp:lossless=true $psrc $we`;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function make_img($src, $q=80) {
|
}
|
||||||
|
|
||||||
|
function make_img($src, $opts="", $q=80) {
|
||||||
$p = img_src($src);
|
$p = img_src($src);
|
||||||
gen_images($p);
|
gen_images($p, $opts);
|
||||||
$path = pathinfo($p);
|
|
||||||
$pwebp = "$path[dirname]/$path[filename].webp";
|
if (pathinfo($p)['extension'] == "png") {
|
||||||
|
$pe = $p;
|
||||||
|
$we = p_img($p, $opts, "webp");
|
||||||
|
|
||||||
echo <<<END
|
echo <<<END
|
||||||
<picture>
|
<picture>
|
||||||
<source srcset="$pwebp" type="image/webp" width=100%/>
|
<source srcset="$we" type="image/webp" />
|
||||||
<source srcset="$p" type="image/jpeg" width=100% />
|
<source srcset="$pe" type="image/png" />
|
||||||
<img src="$p" width=100% />
|
<img src="$pe" />
|
||||||
</picture>
|
</picture>
|
||||||
|
END;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$we = p_img($p, $opts, "webp");
|
||||||
|
$je = p_img($p, $opts, "jpg");
|
||||||
|
|
||||||
|
echo <<<END
|
||||||
|
<picture>
|
||||||
|
<source srcset="$we" type="image/webp" />
|
||||||
|
<source srcset="$je" type="image/jpeg" />
|
||||||
|
<img src="$je" />
|
||||||
|
</picture>
|
||||||
END;
|
END;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function make_bkgd_img($src, $q=80) {
|
|
||||||
|
|
||||||
|
function make_bkgd_img($src, $opts = "", $q=80) {
|
||||||
$p = img_src($src);
|
$p = img_src($src);
|
||||||
|
|
||||||
gen_images($p);
|
gen_images($p, $opts);
|
||||||
$path = pathinfo($p);
|
|
||||||
$pwebp = "$path[dirname]/$path[filename].webp";
|
|
||||||
|
|
||||||
$pe = ltrim($pwebp, '/');
|
$we = p_img($p, $opts, "webp");
|
||||||
$we = ltrim($p, '/');
|
$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)";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue