Wordpress - Create image formats with different qualities when uploading
1) 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 (jpeg) 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 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;
} );
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.
2) 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!