How to Change JPEG Compression in WordPress

Changing WordPress’ default compression of jpeg files is as easy as adding a filter.
What? WordPress automatically compresses JPEG images? This might be news to you. When images are uploaded to WordPress, it will automatically compress jpeg files to 90% of the original. More than likely this is intended to help your pages load faster and keep file sizes smaller. Do you really miss that extra 10% with the high quality of digital images today?

Perhaps you already compress your images as you are preparing them. In this case you may not want WordPress to do anything that might alter the quality. Or maybe your pages are slow and you’d like to be more aggressive in compressing jpeg image files. We’ll show you how to do both.

How to Stop Compressing JPEG Files:

If you want to completely stop compressing jpeg files, you’ll need to tell WordPress to load these file at 100% quality.
Add this to your theme’s functions.php file or create your own functionality plugin for it:

1
add_filter( 'jpeg_quality', create_function( '', 'return 100;' ) );

This will completely disable jpeg compression on your site. However, if you just want to tweak it a bit, you’ll want to read on to the next section.

How to Increase JPEG File Compression:

Compressing your images even further than the default will make your file sizes even smaller and could boost your site’s performance. At a compression rate of 80 or 85%, you may not even notice the difference. The important thing to remember here is that the more the percentage goes down, the more your images will be compressed.

1
add_filter( 'jpeg_quality', create_function( '', 'return 80;' ) );

Please note that any changes you make will only affect new images uploaded to your site. WordPress will not automatically go back to resize all of your previously uploaded images. If you want to update all of your thumbnails with the new sizes, I would suggest one of my favorite plugins of all time – Regenerate Thumbnails. It will quickly go through all of your thumbnails and perform the changes for you.

photo credit: eskimoblood via photopin cc

Tags , ,

Comments (14)

  1. Thanks Sarah! I actually don’t want WordPress to do this because I size my images to exact proportions before uploading them. So I like them to be as is when I upload them.

    Question: i assume but may be wrong. Does WordPress do this for png’s? And would

    `add_filter( ‘png_quality’, create_function( ”, ‘return 100;’ ) );`

    be my solution?

    Kind regards,

      • Marko makes a good point that it’s easy to overlook. If you set the quality to 100 (i.e., no compression) then there’s a very good chance that some of the ‘thumbnails’ may actually have a larger file size than the original uploaded image, which is fairly counterproductive.

        Basically, you will need to play around with compression quality until you get a good balance between file size and image quality.

  2. Hi Sarah

    Question:

    Is it possible to manipulate ‘jpeg_quality’ on an image size by image size basis? (Pardon me for being lazy but) where in the code is this value actually applied during the cropping / resizing process?

    I would presume it’s set for the image being “imported” and then all subsequent resizing / cropping is run off that primary image. As OSFA (one size fits all) that makes sense, but something OSFA might not be ideal.

    Your thoughts? Anybody?

    Thx
    mfs

    • If you write a custom Image Editor you can. There isn’t a hook that fires on every image resize. Not sure if their are a lot of benefits for that since in most cases you also want to call GD or Imagick funcitonality

  3. As the author of EWWW Image Optimizer, I’ve uploaded a LOT of images to wordpress, and I’d like to clarify a couple things:
    1. WordPress does not touch your original image, it is only the resizes that are compressed.
    2. JPG compression levels are not percentages, and they aren’t linear. So, as someone said earlier, setting the quality level to 100 can actually be counterproductive (and it’s still lossy – discarding pixels). And it won’t be much different than quality 90.

    Google’s webpagetest.org actually recommends quality level 50 for web images (which I’m not sure I agree with), so thanks for also mentioning that you can use those filters to reduce the quality level as well.

    Also, FYI: as of 1.7.0, http://wordpress.org/plugins/ewww-image-optimizer/ has implemented an extension for wp_image_editor so that any plugin that uses that class will produce optimized images.

  4. I think the point about 100% “compression” keeps getting buried. To be clear:

    WordPress never stops compressing JPEGs. A setting of 100% still changes them.

    The one upside to the way WordPress handles images is that it keeps the original on the disk and always reuses it when images are recompressed (like with the Regenerate Thumbnails or re-editing with the EWWW plugin).

    Also, if you want to use filters to set the WordPress image quality, then the code above isn’t quite enough. In addition to hooking the ‘jpeg_quality’ filter, you also need to hook ‘wp_editor_set_quality’ as of 3.5. Or … you could use my uber-simple Resized Image Quality plugin: http://wordpress.org/plugins/wp-resized-image-quality/

  5. You wrote: “Please note that any changes you make will only affect new images uploaded to your site. WordPress will not automatically go back to resize all of your previously uploaded images.”

    and you speak about “images”, then you speak about “thumbnails”:

    If you want to update all of your thumbnails with the new sizes, I would suggest one of my favorite plugins of all time – Regenerate Thumbnails. It will quickly go through all of your thumbnails and perform the changes for you.”

    This puzzles me! I’d like to regenerate all “images” and not only thumbnails! Is it possible?

    Many thanks.

Participate