A workaround by extending the WP_Image_Editor_GD
class
The problem is how to access the image sizes before we change the quality of intermediate jpeg images.
Note that the image_resize()
function is deprecated.
We can use the jpeg_quality
filter from the public get_quality
method of the abstract WP_Image_Editor
class:
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
or the wp_editor_set_quality
filter.
Here's one idea how to set the image quality based on the image size (width/height):
/**
* Set image (jpg) quality based on the image size (width/height)
* @see http://wordpress.stackexchange.com/a/165241/26350
*/
add_filter( 'wpse_make_image_arguments', function( $arguments, $filename, $size, $function )
{
// Only target jpeg cases, i.e. with the quality set in $arguments[2]
if( ! isset( $size['height'] ) || ! isset( $size['width'] ) || ! isset( $arguments[2] ) )
return $arguments;
// Modify this part to your needs:
if( $size['height'] <= 150 && $size['width'] <= 150 )
$arguments[2] = 2; // very low quality for easy testing
return $arguments;
}, 10, 4 );
where we've extended the WP_Image_Editor_GD
class:
/**
* Extend the WP_Image_Editor_GD to add the custom wpse_make_image_arguments filter.
* @see http://wordpress.stackexchange.com/a/165241/26350
*/
add_filter( 'wp_image_editors', function( $editors ) {
// Note that the WP_Image_Editor_GD and WP_Image_Editor_Imagick classes
// are included within this filter. So let's do the same for our extension class.
// Our extended class:
class WPSE_Image_Editor_GD extends WP_Image_Editor_GD
{
protected function make_image( $filename, $function, $arguments ) {
// Add a custom filter:
$arguments = apply_filters( 'wpse_make_image_arguments', $arguments, $filename, $this->size, $function );
// Parent method
return parent::make_image( $filename, $function, $arguments );
}
}
// Prepend the extended class to the array of image editors:
array_unshift( $editors, 'WPSE_Image_Editor_GD' );
return $editors;
} );
where we introduced the custom wpse_make_image_arguments
filter.
This way we can modify the quality settings, before the intermediate files are saved.
Here's an example:
PS: I didn't check out the case when the Imagick library is used instead, but I guess we could do something similar by extending the WP_Image_Editor_Imagick
class.