By default, WordPress compresses the versions of images it creates at a JPG quality setting of 82 percent. Here's how you can change it.
WordPress leaves the original images you upload untouched (unless you've used a plugin to specifically override this behavior). But when you add a new image to the media library it also creates extra derivative thumbnail versions that are used throughout the site. There are a few built-in image sizes that you can access and change by going to Settings > Media. They're thumbnail size, medium size, and large size. It's also possible to add more image sizes.
WordPress's JPG compression setting only applies to those derivative thumbnail versions that are created when you upload images to the media library.
It's also important to note that this is, in effect, a cumulative compression. What I mean by that is that you are most likely uploading an image that has already been compression, most likely a JPG. The compression setting in WordPress will apply another round of lossy compression when creating the derivative thumbnails. It is, in effect, double-dipping on the compression. And that's why the default quality is so high--higher than is often necessary for posting on the web.
Why Change WordPress's JPG Quality Setting?
So why would you want to change the default JPG compression setting? There are several possible reasons:
- The cumulative compression might be resulting in images that aren't as sharp as you'd like. So you can increase the quality setting if you like, or even turn it off.
- You might want to make the compression more aggressive to create smaller filesizes. After all, smaller filesizes, especially with data-heavy information like images, can significantly increase your pagespeed. You might find you're quite happy with the image quality at a setting of 70, in which case you'll get even smaller filesizes that make for a faster website that in turn make your site's visitors and the search engines happy.
- You might be applying image optimization with something like WP Smush or ShortPixel. If you're using those services' lossy options, you're going to be applying yet another round of compression that might further degrade the image quality.
How to Customize WordPress's JPG Compression
Setting the compression quality level is as simple as adding a single line of code to your functions.php file.
As always when tinkering with the functions.php, make a backup copy first. That way, if you end up with a white screen, you can simply replace the faulty edited version with the original. And if you've never edited your functions.php before, go into it known that it is possible to mess things up. The most common consequence of that is a white screen of death. Don't panic--it hasn't deleted anything. Just replace the edited functions.php with the safe backup version your created.
So here's how to do it.
Add this single line of code to your function.php, changing the number to your desired setting. 100 means no compression (or as near no compression as makes no meaningful difference). It is, in effect, turning off the compression.
add_filter( 'jpeg_quality', create_function( '', 'return 100;' ) );
You can also turn this around the other direction and add more JPG compression. Simply change the quality setting, like this:
add_filter( 'jpeg_quality', create_function( '', 'return 65;' ) );
The smaller the number, the more compression will be applied. Because it's lossy compression, discarding information does degrade the image quality, so check the results on several images to make sure you're happy with the image quality that's coming out.
What About Existing Images?
This compression setting only applies to new images that uploaded to your media library after you've changed the setting. It doesn't apply to previous images.
But once you're happy with the results of whatever setting you've chose, you can go back and regenerate those older derivative images. The easiest way is to use a plugin. Regenerate Thumbnails is probably the most popular and works well. There are also others.
If you really want to keep your images' filesizes down, image optimization is nearly always a great idea. I've put together some reviews of image optimizers for WordPress.
They work alongside WordPress's own JPG compression. WordPress handles the files created with the initial upload. The image optimizers then work on those files.