Skip to content

TimThumb Quality Settings

Recently I have had a couple of emails about changing the amount of compression on TimThumb cropped images. The change itself is really easy, however I thought I would write it up as a reference for those who aren’t aware of it’s existence.

To change the level of compression all you have to do is edit the ‘q’ (which stands for quality) parameter in the query string.

If you don’t set the value then a default value of 80 will be used. This generally gives a good trade off between speed and quality, however if you are running a photo site or need to optimize your images for speed, then perhaps you will want to tweak things.

The allowed values are between 0 and 100.

  • q=0 is the most compression you can have on the image
  • q=100 is the least compression – totally uncompressed

Personally I would never use a quality of 100, it will increase the file size massively for little to no visible gain. If you want to increase the quality then around 95 is the highest you would need to go. The quality level there should be enough for most people.

Share

Join me, Laura, Gary, Manifo. They're all chatting about "TimThumb Quality Settings" below ›

Read Comments

11 Comments »

  1. I was in this little not aware group of people, so thanks for that ;) Sometimes it is realy good to write about something that most of us would seen as obvious.

    • Glad it helped, and you’re totally right. There’s lots of things I take for granted that I forget others don’t know that could help them. I will always try to share these things if I can (if I think of them) :)

  2. Hi!

    I have a problem when resizing images that are smaller than the width/height parameters on the Timthumb request.

    The thing is I’m using images from different (*external*) sources, and usually those images are bigger than my width/height and the resizing works perfect, but sometimes they are smaller, and I don’t want Timthumb to make them bigger, because then they look pixeled and awful…

    I’ve looked at the code and have tried to modify the MAX_WIDTH and MAX_HEIGHT constants based on new parameters (w2 and h2) but it didn’t work (strange)

    $max_width = (int) abs (get_request (‘w2′, 0));
    $max_height = (int) abs (get_request (‘h2′, 0));
    if ($max_width > 0) define (‘MAX_WIDTH’, $max_width);
    if ($max_height > 0) define (‘MAX_HEIGHT’, $max_height);

    // ensure size limits can not be abused
    $new_width = min ($new_width, MAX_WIDTH);
    $new_height = min ($new_height, MAX_HEIGHT);

    Thanks for this script, I’ve been using phpthumb for years and was really tired of it, and today I found it had a security bug (it was ignoring the nohotlinking parameters to allow only certain external webpages and just accepting everything, so dangerous!!!) and found out about Timbthumb, much simpler and effective, great! :)

  3. Errr actually, that max_width max_height thing was a bit of a nonsense try…

    What I want is the image to show at its real size, in the case when its width and/or height is smaller than the width/height parameters, to avoid pixeling at all

  4. I have a problem. Finally got TT working after no thumbnail generation. However, now it’s defaulting to 10% quality even with this >>
    if(! defined(‘DEFAULT_Q’) ) define (‘DEFAULT_Q’, 75);

    Doesn’t matter what i set it to, still defaults to 10%. Any ideas? Not sure where it’s getting 10% from?

    An example is here >> http://lumenarte.co.uk/wp-content/themes/mio/sp-framework/timthumb/timthumb.php?src=http://lumenarte.co.uk/wp-content/uploads/2012/11/Aero_parete_2.jpg&h=500&w=500&zc=0&q=10&a=c&s=&f=&cc=&ct=

    Thanks if you can help.

    • You shouldn’t be editing the code. The quality parameter is et in the timthumb parameters and you have set the quality (q value) to 10. The define is the default if q is not set.

  5. Hi!

    Could you please explain me the reason why a Timthumb cache file weights 386 KB for a 117 KB original file?
    Timthumb 2.8.11 quality parameter is set to 90%.

    Also, a strange thing that confuses me a lot is that if I set the quality parameter to 30%
    if(! defined(‘DEFAULT_Q’) ) define (‘DEFAULT_Q’, 30);
    and remove the cached file of the above-mentioned image and refresh my browser, the regenerated image is ugly (this is logical) but its cache file weights 409 KB!?!

    Thank you for explaining this mystery, if possible.
    Regards,

    Eric

  6. I’ve 2 questions:

    1. To use your q quality fix, do I change this throughout the theme templates, or is this changeable at one place within the timthumb script itself?

    2. Same as above poster Eric asks, I’m recommended to optimize my images and it points to timthumb versions could be optimized a further 20%. However checking the timthumb cache these are 2-3 times larger than the image I’ve originally uploaded in the media folder! Why is this bloat and do you have any recommendations on how to fix?

Comments are closed. Let's continue the conversation on Twitter.
css.php