Skip to content

Commit 058c237

Browse files
authored
Enable custom parameters for .webp and .avif thumbnails (#876)
* Enable custom parameters for .webp and .avif thumbnails The thumbnail code assumed that images are .gif, .png or .jpg, and we defaulted to quality settings for .jpg thumbnails. But for both .avif and .webp, other quality settings are preferable (and possible other additional parameters). This code changes the code for $serendipity['imagemagick_thumb_parameters'] so that it can be an array, with the image mime type as key and their specific parameters as values. It also sets sane defaults for the gd path for both formats, which also enables thumbnails in both formats when imagemagick is not used. * Document change
1 parent f425f7f commit 058c237

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

docs/NEWS

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Version 2.6-alpha1 ()
33
* Avoid PHP 8 warning about incurrect reference return of db_query
44
function
55

6+
* Enable .webp and .avif thumbnails also when using PHP's gd module
7+
instead of imagemagick. Optimize imagemagick's thumbnail parameters,
8+
for smaller filesizes for .webp and .avif images.
9+
NOTE: Check the new structure in serendipity_config.inc.php if you
10+
have set a custom value for imagemagick_thumb_parameters in your
11+
local config file.
12+
613
* PHP 8.4 fix: Remove deprecated constant E_STRICT
714

815
* PHP 8 compatibility fixes for bundled XML/RPC.php

include/functions_images.inc.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,22 @@ function serendipity_makeThumbnail($file, $directory = '', $size = false, $thumb
735735
$newSize .= '>'; // tell imagemagick to not enlarge small images, only works if safe_mode is off (safe_mode turns > in to \>)
736736
}
737737
if (!$serendipity['imagemagick_nobang']) $newSize .= '!'; // force the first run image geometry exactly to given sizes, if there were rounding differences (see https://github.com/s9y/Serendipity/commit/94881ba4c0e3bdd4b5fac510e93977e239171c1c and comments)
738-
$cmd = escapeshellcmd($serendipity['convert'] . ' ' . $serendipity['imagemagick_thumb_parameters']) . ' -antialias -resize ' . serendipity_escapeshellarg($newSize) . ' ' . serendipity_escapeshellarg($infile) .' '. serendipity_escapeshellarg($outfile);
738+
739+
$params = '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85';
740+
if (is_string($serendipity['imagemagick_thumb_parameters'])) {
741+
// In s9y < 2.6, $serendipity['imagemagick_thumb_parameters'] was a string and the default
742+
// value targeted jpgs. Users might have set variants of that in their local config, so
743+
// we provide a backwards compatibly path here
744+
$params = $serendipity['imagemagick_thumb_parameters'];
745+
} else {
746+
// In 2.6 $serendipity['imagemagick_thumb_parameters'] has values for different
747+
// image formats
748+
if (isset($serendipity['imagemagick_thumb_parameters'][$fdim['mime']])) {
749+
$params = $serendipity['imagemagick_thumb_parameters'][$fdim['mime']];
750+
}
751+
}
752+
753+
$cmd = escapeshellcmd($serendipity['convert'] . ' ' . $params) . ' -antialias -resize ' . serendipity_escapeshellarg($newSize) . ' ' . serendipity_escapeshellarg($infile) .' '. serendipity_escapeshellarg($outfile);
739754
}
740755
exec($cmd, $output, $result);
741756
if ($result != 0) {
@@ -1318,6 +1333,18 @@ function serendipity_functions_gd($infilename) {
13181333
$func['save'] = 'imagejpeg';
13191334
$func['qual'] = 85;
13201335
break;
1336+
1337+
case 'avif':
1338+
$func['load'] = 'imagecreatefromavif';
1339+
$func['save'] = 'imageavif';
1340+
$func['qual'] = 60;
1341+
break;
1342+
1343+
case 'webp':
1344+
$func['load'] = 'imagecreatefromwebp';
1345+
$func['save'] = 'imagewebp';
1346+
$func['qual'] = 90;
1347+
break;
13211348

13221349
case 'png':
13231350
$func['load'] = 'imagecreatefrompng';

serendipity_config.inc.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@
496496
// image quality when resizing.
497497
//
498498
// Set a variable like below in your serendpity_config_local.inc.php with your own settings
499-
$serendipity['imagemagick_thumb_parameters'] = '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85 -interlace JPEG';
499+
$serendipity['imagemagick_thumb_parameters'] = [ 'image/jpeg' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85 -interlace JPEG',
500+
'image/avif' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 60',
501+
'image/webp' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 90'
502+
];
500503
}
501504

502505
serendipity_plugin_api::hook_event('frontend_configure', $serendipity);

test.avif

12.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)