Cant comment, have to post together with another version.
The problem(s):
Im not shure if the Update 2) Set jpeg quality per image size name in the accepted answer is complete. wp_get_additional_image_sizes()
returns the global $_wp_additional_image_sizes
and does not contains any information about default sizes as medium, large or thumbnail.
This might not work in the filter: 'large' => 2
As of https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes explains with custom code to retrive ALL sizes with a custom function.
Am I right?
Second, The idea of filter diffrent "sizes" by name is tricky. _resize( $max_w, $max_h, $crop = false )
only understand and parses matched width, height and crop value, and every match suppose to match a name. But in many cases the name will be "another" name.
A "medium" image setting that has a equal "shop_product" setting will only "visit" this function as one of them. Then we wont know if it is "medium" size or "shop_product" size thats being filtered. Because, the objectives here is just a technical crop of an image.
The idea with Update 2) is a more logic handeling, but Im afraid that the techncal architecure is not there. To analyze all registered image sizes before constructing the filter, becomes more development to make shure the intention returns the correct quality image program on current Theme.
All size names with equal settings will share the same new quality as
they share the same image on the server.
So, if you still stick to the update 2) I think you need to call another function to populate the $sizes
variable, and analyse by var_dump() the $_wp_additional_image_sizes
for equal settings.
The approach in 1) A workaround by... is more logic/ semantic. We solved this by the same extending method, but using a dynamic filter instead:
add_filter('wp_image_editors', function($editors){
if(!class_exists('ENTEX_Image_Editor_GD')){
class ENTEX_Image_Editor_GD extends WP_Image_Editor_GD {
private $wp_quality = null;
protected function _resize($max_w, $max_h, $crop = false){
$condition = ($crop ? 'true' : 'false');
if($this->wp_quality === null) $this->wp_quality = $this->get_quality();
$quality = apply_filters('wp_editor_set_quality_'. $max_w .'x'. $max_h .'_'. $condition, $this->wp_quality);
$this->set_quality((int) $quality);
return parent::_resize( $max_w, $max_h, $crop );
}
}
}
array_unshift($editors, 'ENTEX_Image_Editor_GD');
return $editors;
});
To filter each individual image size we then use:
add_filter('wp_editor_set_quality_160x160_true', function($quality){
return 96;
});
Whats also diffrent from this extended version is that we are using/ populates the add_filter('jpeg_quality' ...
version as default value.
Here are some other examples of custom filters:
function high_quality_on_medium_images(){
$in = 'medium';
$max_w = get_option($in.'_size_w');
$max_h = get_option($in.'_size_h');
$crop = get_option($in.'_crop');
$condition = ($crop ? 'true' : 'false');
add_filter('wp_editor_set_quality_'. $max_w .'x'. $max_h .'_'. $condition, 'my_theme_high_quality_images', 10, 1);
}
add_action('after_setup_theme', 'high_quality_on_medium_images', 99);
function my_theme_high_quality_images($quality){
return (int) 82;
}
Developers attention
- Make shure filters are applied after the Theme images setup.
- Dont wrap inside
is_admin()
then ajax call, front end, remoted
posted images wont bite.
As mentioned, the crop value could be either 0 or false, you must convert (int) or (bool) into a string with $condition = ($crop ? 'true' : 'false');
Theme developers attention
The WP default compression is quite progressive. Values between 80
and 99
could return a huge difference or no visible result at all. A none comression value as 100
, likely returns a bigger filesize on "smaller sized" 800 px images, then the original big 3000 px sized version.
After many years we found a magic number of 96, the filesize get smaller but you cant see the diffrence between a 100
value compression.
High quality photoshop prepared shop images, bigger then 800px is fine with Wordpress default 82. But iPhone "Large" email posted image really need a 96 value on SMALL sizes like thumbnails. They become "soften blur" on Wordpres compressions.
You will over-do with this topic solutions if you not planning the
scenarios for your project, users or Theme.
A final reflexion
Im really dissapointed at Wordpress that this important issue is neglected in the priority of development, as images are more then ever important to be unique effective onload.
Wy cant we just get a filter in right place that can be used for individual compression? They bother to "change the name" to wp_editor_set_quality, and add_filter('jpeg_quality' ...
is now deprecated. Why not think this trought all the way and just remove the if ( ! $this->quality )
only check once method as mentioned by @birgire.