A workaround by extending the WP_Image_Editor_GD
class
- A workaround by extending 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 extended class.
// Our extended class that overrides the WP_Image_Editor_GD::make_image() method
if( ! class_exists( 'WPSE_Image_Editor_GD' ) )
{
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;
} );
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.
- Update - Set jpeg quality per image size name
Here's another version where we set the jpeg quality per image size name:
/**
* Extend the WP_Image_Editor_GD to set quality per image size name.
*
* @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 extended class.
// Our extended class that overrides the WP_Image_Editor_GD::_resize() method
if( ! class_exists( 'WPSE2_Image_Editor_GD' ) )
{
class WPSE2_Image_Editor_GD extends WP_Image_Editor_GD
{
protected function _resize( $max_w, $max_h, $crop = false )
{
$qualities = apply_filters( 'wpse_jpeg_qualities', [] );
$default_quality = (int) apply_filters( 'wpse_default_jpeg_quality', 82 );
$sizes = wp_get_additional_image_sizes();
$this->set_quality( $default_quality );
foreach( (array) $qualities as $name => $quality )
{
if(
isset( $sizes[$name] )
&& (int) $sizes[$name]['width'] === (int) $max_w
&& (int) $sizes[$name]['height'] === (int) $max_h
&& (bool) $sizes[$name]['crop'] === (bool) $crop
)
$this->set_quality( $quality );
}
// Parent method
return parent::_resize( $max_w, $max_h, $crop );
}
}
}
// Prepend the extended class to the array of image editors:
array_unshift( $editors, 'WPSE2_Image_Editor_GD' );
return $editors;
} );
I noticed that the crop arguments can be 0, false or empty, so we do some typecasting to be sure.
Here we've introduced the following new filters:
add_filter( 'wpse_jpeg_qualities', function( $qualities )
{
return [ 'hello-image' => 2, 'medium' => 2, 'large' => 2 ];
} );
and
add_filter( 'wpse_default_jpeg_quality', function( $quality )
{
return 82;
} );
that can hopefully be adjusted to your needs!