Last updated on September 24, 2023

Disable Image Compression in WordPress

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Maintain image quality; disable compression.

To disable image compression in WordPress, you can use the jpeg_quality filter hook to set the quality of JPEG images to 100. By default, WordPress compresses JPEG images to 82% quality. Disabling image compression can be useful in scenarios where you want to maintain the original quality of your images without any loss due to compression.

Here’s an example code snippet that disables image compression in WordPress:

function wpsnippets_disable_image_compression( $quality ) {
    return 100;
}
add_filter( 'jpeg_quality', 'wpsnippets_disable_image_compression' );

In the code snippet above, we define a custom PHP function wpsnippets_disable_image_compression that takes the $quality parameter. We set the value of $quality to 100, indicating that we want to disable image compression and maintain the maximum quality for JPEG images.

We then use the add_filter function to hook our custom function wpsnippets_disable_image_compression to the jpeg_quality filter. This filter is responsible for determining the quality of JPEG images generated by WordPress.

By adding this code snippet to your WordPress theme’s functions.php file or a custom plugin, you can effectively disable image compression for JPEG images in WordPress.

Examples

Example 1: Disable image compression for all uploaded images

This code example demonstrates how to disable image compression for all images uploaded to WordPress. By adding this code to your theme’s functions.php file, you can ensure that the compression algorithm is not applied to any uploaded images.

function wpsnippets_disable_image_compression() {
    add_filter( 'jpeg_quality', function( $quality ) {
        return 100;
    }, 10, 2 );
}
add_action( 'after_setup_theme', 'wpsnippets_disable_image_compression' );

Explanation: The wpsnippets_disable_image_compression function is hooked to the after_setup_theme action, ensuring that it runs after the theme is set up. Inside the function, we use the add_filter function to modify the jpeg_quality filter. We define an anonymous function that returns a quality value of 100, effectively disabling image compression for all uploaded JPEG images.

Example 2: Disable image compression for specific image sizes

This code example demonstrates how to disable image compression for specific image sizes in WordPress. By modifying the jpeg_quality filter for specific image sizes, you can control the compression level for each size individually.

function wpsnippets_disable_image_compression_for_sizes( $sizes ) {
    $sizes[] = 'custom-size';
    return $sizes;
}
add_filter( 'jpeg_quality_for_size', 'wpsnippets_disable_image_compression_for_sizes' );

Explanation: The wpsnippets_disable_image_compression_for_sizes function is hooked to the jpeg_quality_for_size filter. It accepts an array of image sizes and appends a custom size, ‘custom-size’, to the array. This ensures that the custom size will not undergo image compression when generated.

Example 3: Disable image compression for specific image formats

This code example demonstrates how to disable image compression for specific image formats in WordPress. By modifying the wp_editor_set_quality filter, you can control the compression level for different image formats.

function wpsnippets_disable_image_compression_for_formats( $quality, $mime_type ) {
    if ( $mime_type === 'image/png' ) {
        return 100;
    }
    return $quality;
}
add_filter( 'wp_editor_set_quality', 'wpsnippets_disable_image_compression_for_formats', 10, 2 );

Explanation: The wpsnippets_disable_image_compression_for_formats function is hooked to the wp_editor_set_quality filter. It accepts the quality value and the MIME type of the image being processed. In this example, we check if the MIME type is ‘image/png’ and return a quality value of 100, effectively disabling compression for PNG images. For other image formats, the original quality value is returned, allowing compression to be applied.

Last updated on September 24, 2023. Originally posted on September 27, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *