WordPress JPEG Thumbnail Image Quality Setting and Adjustment
by Glyn Mooney
Well, I had to do some digging around deep in the WordPress internals for this one! If you want to set the image quality of the thumbnails from the “the_post_thumbnail” function/template tag add the following line to your functions.php file to override the default value of 90%:
add_filter('jpeg_quality', function($arg){return 100;});
On some production environments you may get a “unexpected T_FUNCTION” error (older versions of PHP 5 don’t like anonymous functions). In this situation go for the following:
function jpeg_quality_callback($arg)
{
return (int)100;
}
add_filter('jpeg_quality', 'jpeg_quality_callback');
{
return (int)100;
}
add_filter('jpeg_quality', 'jpeg_quality_callback');
Here I’m setting the quality to 100%, but you can set it to whatever value suits your project. You have to set the “$arg” to swallow the argument that get’s passed to the original function in the “image_resize” function in the media.php file.
Hope this helps! Leave a comment if it did!
Is pasting the second code into the functions.php is enough? Or do we need to make some modifications?
No just Ctrl+C Ctrl+V this code in and it works off the shelf for you :)
thank you :)
No problem :)
[...] awesome trick by Glyin Mooney over at Skidoosh.co.uk for changing the JPEG quality in WordPress (original article). Basically you can add a simple function to your functions.php file to define the default JPEG [...]
Dude this is a freaking awesome tip, I used to go in and mess with my media.php file – lol.
I’ve mentioned you in a recent post if you want to check it out: http://www.wpexplorer.com/blog/optimize-images-wordpress.html
Thanks mate :) I had to go digging round in the gut’s to find this one! Always good to know it helped someone :)
[...] this method is outdated ! There is a much better solution at Skidoosh ! [...]
Thanks Glyn Mooney, I have change my wordpress blog based on post above. It works..I am happy now get my pictures on my blog look detail
I cant find functions.php in my editor at all (wordpress 3.2.1)…
It’s is located in your theme folder. If it’s not there you’ll have to create the file. Thanks!
Thanks for the feedback!
I couldn’t get this to work. I pasted the code into my functions.php and regenerated my thumbnails and it didn’t work. Anyone else have any ideas on how I can fix this?
Hi Mawk,
What version of WordPress are you using?
I’m using 3.2.1
Theme: Sidewinder from Graph Paper Press
Hi Mark,
Could you regenerate your thumbnails again and check the file time on them to ensure they are actually being regenerated?
Can you tell me you PHP version number and which variation of the code your using?
The second set of command worked like a charm. I set the quality to 100%, though it increased the file size but the quality is a lot better. Can you please tell me the default image quality? maybe 90?
Yes… Sorry, not a particularly long reply :)
thumbnails still blurry in many places.
Hi Paul. You need to regenerate your thumbs to complete the process.
Hi there, I wondering if you could help me.
I tired the first piece of code and it said t-function error, so I tried the second piece of code. It won’t load and says ‘Fatal error: Call to undefined function add_filter() in /websites/123reg/LinuxPackage21/co/un/ci/councilpop.me.uk/public_html/wp-includes/functions.php on line 4689′
Is it because of where I have pasted the code? (at the bottom of the file before the ?> )
Any help would be largely appreciated
take care
Anthony
Hi Anthony,
I don’t think you did anything wrong. What version of PHP are you running, and what version of WordPress?
P.S. Could you check to see that the function actually exists in wp-includes/functions.php?
Hi there, thanks for the reply :)
I am not quite sure what version of php, but I use http://www.reg-123.co.uk and I found this in there help section
“We have PHP 4.4.8 and 5.2.5 installed on our servers. You can change between PHP versions in your MyPanel.”
Does this make sense?
And yes I did installed it into the wp-includes section’
In addition I will leave a link to the website, just so you can see my visual issue : http://www.councilpop.me.uk
Thanks for any help mate :)
I’ve just messaged you on your blog mate :)
Thanks Glyn! I was having a hard time with my travel blog because of the reduction in the image quality. But thanks to what your tip now I can go back to travel blogging with detail in my images. :)
No problem! Thanks for the comment!