The webp files produced from png originals are suboptimal. The reason is that these are derived from a jpg version (with 100% quality).

So instead of a crystal clear png→webp of 3KB we get a png→jpg→webp of 20KB with jpg artifacts present.

Comments

ioannis.cherouvim created an issue. See original summary.

pedrop’s picture

I think this is also sub-optimal for jpg to webp conversion as usually image styles and effects are downscaling the image.
As these conversions are stored in temporary jpeg files the conversion to WebP will not harness the full potential of the superior WebP compression as opposed to a direct, clean, single-step original JPEG to WebP scale/crop and convert command.
I tried to gather this topic in a blog post: https://www.brainsum.com/blog/webp-right-way-and-wrong-way-drupal

weseze’s picture

Could you please clarify the "a png→jpg→webp of 20KB with jpg artifacts present"?
As far as I can see png's are not converted to jpg prior to the web conversion. The png source is loaded and passed to the webp conversion function directly.

As for the jpg to webp conversion: that really is a bummer...
Would be much better if this module reversed the behaviour, creating a webp first and then deriving a jpg/png from it.

weseze’s picture

StatusFileSize
new2.03 KB

I did some more digging into this and it is possible to have PNG's compress lossless.
BUT, this required PHP8.1.x and upwards. (and also a certain version of GD which comes bundled with PHP)
Attached is a patch that checks if the functionality is available and if so provides an extra option to compress all PNG files lossless.

This is not a complete fix for this issue, but it at least fixes quality issues with PNG files. Usually you create PNG instead of JPG with the sole purpose of having lossless compression. (it least it is what we do)

weseze’s picture

StatusFileSize
new2 KB

Updated patch, still had a debug in there.

martijn de wit’s picture

Status: Active » Needs review

patch is available so change issue status.

michaellander’s picture

@weseze, can you clarify the part that this patch is missing? Is it the idea that cropping/scaling a JPG image prior to conversion, automatically introduces compression, even if set to 100% quality?

My hands might be tied from this modules perspective, as we are piggybacking pipelines which naturally happens after image styles are applied. If I had the time, I'd create a module(or better yet, core functionality) whose sole purpose is to generate derivatives and then allow support for multiple formats(AVIF for example) so that ImageAPI optimize can go back to just optimizing and not the derivative piece.

weseze’s picture

StatusFileSize
new2 KB

@michaellander: sorry for only getting back to this now, I must have missed your update...

Not sure what you mean by "the part that this patch is missing"? The patch itself is not "missing" anything. What it adds is the option to allow png files to be compressed directly and lossless to webp. It does not affect jpg to webp conversion.

Also, attached is a patch that is compatible with Drupal 11. @see: https://www.drupal.org/project/imageapi_optimize_webp/issues/3504713