jpeg_quality
Sets the compression quality of generated JPG files.
By default, WordPress creates files with 82% compression quality. This filter allows the value to be decreased or increased.
In most cases, there is no need to change this parameter. However, to save a few kilobytes, setting the quality to 80 or 70 may be useful.
Changing this parameter affects only the quality of thumbnails created from the original; the original itself remains untouched...
Usage
add_filter( 'jpeg_quality', 'wp_kama_jpeg_quality_filter', 10, 2 );
/**
* Function for `jpeg_quality` filter-hook.
*
* @param int $quality Quality level between 0 (low) and 100 (high) of the JPEG.
* @param string $context Context of the filter.
*
* @return int
*/
function wp_kama_jpeg_quality_filter( $quality, $context ){
// filter...
return $quality;
}
- $quality(integer)
- The compression quality. It can range from 0 (maximum compression) to 100 (no compression).
Examples
#1 Usage example
Suppose you are a photographer who runs a blog. The quality of published photos is reduced, but WordPress must process the images without reducing their quality, meaning 100% quality is required.
Use this hook by adding the following code to the theme's functions.php file:
add_filter( 'jpeg_quality', 'filter_function_name_11' );
function filter_function_name_11( $quality ){
return 90;
}
It can be written more concisely (for PHP versions above 5.3):
add_filter( 'jpeg_quality', function( $quality ){
return 100;
} );
#2 PHP 7.4 version
add_filter( 'jpeg_quality', fn ( $q ) => 100 );
Changelog
| Since 2.5.0 | Introduced. |
Where the hook is called
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
$quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' );
return imagejpeg( $image, $filename, apply_filters( 'jpeg_quality', 90, 'edit_image' ) );