Is it possible to set the image quality based on the image size? I’d like to have better image quality for larger images (80) – and worse for small thumbnails (30).
I was expecting a parameter at add_size
to control that – but there is none.
If it matters: I’m using ImageMagick.
The only time setting the quality really matters is right before the image is saved or streamed (for the editor). Both of these have the “image_editor_save_pre” filter there, passing it the instance of the image editor. So you can use that to modify the image in any way you like, including setting the quality.
So, something like this should do the job simply and easily:
Note upfront: Below answer isn’t finished and is not tested, but I ainĀ“t got enough time left, so I’ll leave this here as draft. What probably needs a second pair of eyes is the quality method and the interpretation of
version_compare()
.First we need an entry point. After reading through the make post again, I thought best would be to jump in before the Image Editor saves the newly created image. So here’s a micro controller that intercepts during a callback hooked to
image_editor_save_pre
and loads a class that then walks through your settings defined inside a callback towpse_jpeg_quality
. It simply returns different compression ratios for thejpeg_quality
filter that runs inside the Image Editor.The actual worker is the
JPEGQualityWorker
class. It resides in the same directory as above main plugin file and is namedworker.php
(or you change the controller above).It retrieves the image and your settings and then adds callbacks to the
jpeg_quality
filter. What is does isThe breakpoint and the limit is what decide between high and low and as mentioned above this might need some more love.