Skip to content

Commit c10fa2a

Browse files
committed
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.
1 parent a6619f7 commit c10fa2a

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

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
@@ -503,7 +503,10 @@
503503
// image quality when resizing.
504504
//
505505
// Set a variable like below in your serendpity_config_local.inc.php with your own settings
506-
$serendipity['imagemagick_thumb_parameters'] = '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85 -interlace JPEG';
506+
$serendipity['imagemagick_thumb_parameters'] = [ 'image/jpeg' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85 -interlace JPEG',
507+
'image/avif' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 60',
508+
'image/webp' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 90'
509+
];
507510
}
508511

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

test.avif

12.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)