From d93f65ecf4924bc5351568ba2fc70b87e45143f7 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sun, 17 Oct 2021 00:01:05 +0200 Subject: [PATCH] dieren: high-resolution webp image alternatives Modern compression format allows for much higher quality at the same size, enough for (1920px / 3 columns) or 2x DPR at previous 320px width, even then with generally less visible artifacts than JPEG; in direct comparison only pig (and its porpoise sibling) are significantly worse due to lost details. Automatically served by Apache if advertised in client Accept header (currently supported by 95% ie all modern browsers). --- .htaccess | 5 +++++ tools/mkimgthumb | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.htaccess b/.htaccess index f9d21dd..35296ee 100644 --- a/.htaccess +++ b/.htaccess @@ -21,6 +21,11 @@ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{DOCUMENT_ROOT}/$1.plp -f RewriteRule ^/*([^/]+)(.*) $1.plp$2 +# replace jpeg images by webp alternatives if supported +RewriteCond %{HTTP_ACCEPT} \bimage/webp +RewriteCond %{DOCUMENT_ROOT}/$1.webp -f +RewriteRule (.*)\.jpg$ $1.webp + # allow browsers to cache for upto a month diff --git a/tools/mkimgthumb b/tools/mkimgthumb index fc33162..217b773 100755 --- a/tools/mkimgthumb +++ b/tools/mkimgthumb @@ -22,9 +22,11 @@ for my $src (@ARGV) { unshift @cmds, -chop => "$crop[0]%x$crop[1]%"; } unshift @cmds, -gravity => 'northwest' if @cmds; - push @cmds, -resize => '300x200^', -gravity => 'north', -extent => '300x200'; - push @cmds, '-strip', -quality => '60%'; - system(convert => $src, @cmds, "../$name.jpg") == 0 + push @cmds, '-strip', -gravity => 'north', -quality => 60; + my @cmdwebp = (-resize => '630x420^', -extent => '630x420', -quality => 30); + my @cmdjpeg = (-resize => '300x200^', -extent => '300x200'); + system(convert => $src, @cmds, @cmdjpeg, "../$name.jpg" ) == 0 and + system(convert => $src, @cmds, @cmdwebp, "../$name.webp") == 0 or $failcount += warn "error creating $name.jpg from $src\n"; } -- 2.30.0