Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom parameters for .webp and .avif thumbnails #876

Merged
merged 3 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Version 2.6-alpha1 ()
------------------------------------------------------------------------

* Enable .webp and .avif thumbnails also when using PHP's gd module
instead of imagemagick. Optimize imagemagick's thumbnail parameters,
for smaller filesizes for .webp and .avif images.
NOTE: Check the new structure in serendipity_config.inc.php if you
have set a custom value for imagemagick_thumb_parameters in your
local config file.

* PHP 8.4 fix: Remove deprecated constant E_STRICT

* PHP 8 compatibility fixes for bundled XML/RPC.php
Expand Down
29 changes: 28 additions & 1 deletion include/functions_images.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,22 @@ function serendipity_makeThumbnail($file, $directory = '', $size = false, $thumb
$newSize .= '>'; // tell imagemagick to not enlarge small images, only works if safe_mode is off (safe_mode turns > in to \>)
}
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)
$cmd = escapeshellcmd($serendipity['convert'] . ' ' . $serendipity['imagemagick_thumb_parameters']) . ' -antialias -resize ' . serendipity_escapeshellarg($newSize) . ' ' . serendipity_escapeshellarg($infile) .' '. serendipity_escapeshellarg($outfile);

$params = '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85';
if (is_string($serendipity['imagemagick_thumb_parameters'])) {
// In s9y < 2.6, $serendipity['imagemagick_thumb_parameters'] was a string and the default
// value targeted jpgs. Users might have set variants of that in their local config, so
// we provide a backwards compatibly path here
$params = $serendipity['imagemagick_thumb_parameters'];
} else {
// In 2.6 $serendipity['imagemagick_thumb_parameters'] has values for different
// image formats
if (isset($serendipity['imagemagick_thumb_parameters'][$fdim['mime']])) {
$params = $serendipity['imagemagick_thumb_parameters'][$fdim['mime']];
}
}

$cmd = escapeshellcmd($serendipity['convert'] . ' ' . $params) . ' -antialias -resize ' . serendipity_escapeshellarg($newSize) . ' ' . serendipity_escapeshellarg($infile) .' '. serendipity_escapeshellarg($outfile);
}
exec($cmd, $output, $result);
if ($result != 0) {
Expand Down Expand Up @@ -1318,6 +1333,18 @@ function serendipity_functions_gd($infilename) {
$func['save'] = 'imagejpeg';
$func['qual'] = 85;
break;

case 'avif':
$func['load'] = 'imagecreatefromavif';
$func['save'] = 'imageavif';
$func['qual'] = 60;
break;

case 'webp':
$func['load'] = 'imagecreatefromwebp';
$func['save'] = 'imagewebp';
$func['qual'] = 90;
break;

case 'png':
$func['load'] = 'imagecreatefrompng';
Expand Down
5 changes: 4 additions & 1 deletion serendipity_config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,10 @@
// image quality when resizing.
//
// Set a variable like below in your serendpity_config_local.inc.php with your own settings
$serendipity['imagemagick_thumb_parameters'] = '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85 -interlace JPEG';
$serendipity['imagemagick_thumb_parameters'] = [ 'image/jpeg' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 85 -interlace JPEG',
'image/avif' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 60',
'image/webp' => '-sampling-factor 4:2:0 -unsharp 0x0.75+0.75+0.008 -strip -quality 90'
];
}

serendipity_plugin_api::hook_event('frontend_configure', $serendipity);
Expand Down
Binary file added test.avif
Binary file not shown.