Add generic image conversion property; generates images to a /gen/ folder.
This commit is contained in:
parent
3953a4eb2a
commit
9f6302fa48
108
router.php
108
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
|
||||
<picture>
|
||||
<source srcset="$we" type="image/webp" />
|
||||
<source srcset="$pe" type="image/png" />
|
||||
<img src="$pe" />
|
||||
</picture>
|
||||
END;
|
||||
} else {
|
||||
|
||||
$we = p_img($p, $opts, "webp");
|
||||
$je = p_img($p, $opts, "jpg");
|
||||
|
||||
echo <<<END
|
||||
<picture>
|
||||
<source srcset="$pwebp" type="image/webp" width=100%/>
|
||||
<source srcset="$p" type="image/jpeg" width=100% />
|
||||
<img src="$p" width=100% />
|
||||
<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);
|
||||
|
||||
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)";
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue