Home/ Journal Bens Projects Binary Moon Archives About Ben Gillbanks Subscribe to Binary Moon Updates

Subscribe to Binary Moon Website Updates, it's Free and Easy to Stay in Touch

Search Binary Moon

Search Binary Moon

TimThumb Proportional Scaling

Moving the crop position used to be the most requested TimThumb feature - but then I added it, and so something else had to step up and replace it.

The next most requested feature is proportional image scaling. To be honest, this feature request had me confused for a while, I just couldn't understand what people were asking for. TimThumb already had the ability to scale images proportionally, simply by supplying a single size parameter. However what, it turns out, was wanted - was being able to scale down the image so that the image remains in proportion, and it fits into the required dimensions - adding borders where necessary.

So that's what I added!

Proportional Image Scaling

Usage is simple. The mysterious zc (Zoom & Crop) parameter comes into action, simply give it the value of 2 and it will apply the borders as required.

zc = Zoom & Crop

The zc parameter was introduced to TimThumb about 3 months after it was first published. The reason for the inclusion is that the scaling in the original version was purely scaling, there was no cropping. This meant that images could end up severely distorted.

When we fixed this feature bug the zc parameter was introduced. The idea being that existing websites would continue to work as they were. As time went on it seemed to be less and less relevant, until it got to the stage where I was considering removing it entirely.

With the new cropping modes this thought has ended. I have added some new scaling modes, as described below.

0Resize to Fit specified dimensions (no cropping)
1Crop and resize to best fit the dimensions (default)
2Resize proportionally to fit entire image into specified dimensions, and add borders if required
3Resize proportionally adjusting size of scaled image so there are no borders gaps

Scaling Demo

I have put together a simple demo showing how this Zoom and Scaling functionality works. IMO seeing it visually is a lot easier to understand than having it written down.

What's next?

Now that the most requested features have been implemented, what more do I have to offer? I am sure something else will crop up, but I am not sure what it will be. However I am really interested to see what is to come.

18 Responses to “TimThumb Proportional Scaling” Leave a reply ›

  • Is there a parameter to control the color of the borders? even better could they be set to transparent?

    • Profile

      There's nothing like this currently, but I have considered it and will likely add the colour setting. Not sure about the transparency option. That might get confusing when resizing jpgs (that aren't transparent).

  • Hi,

    I'm configuring a blog using the Isotherm theme (WordPress) and I cannot get the TimThumb work.

    The images are not cropped, but showing resized and stretched: http://superbiz.com.br/~supclubc/

    How can I fix it?

  • Hi,
    This is a great plugin, I'm using that, and helped me a lot.
    Is there any parameter to delete the file, after create the thumbnail to save disk space?

    Thank you.

    • Profile

      Hi - there is nothing for deleting the cache file. The cache file directory is emptied over time by TimThumb (to save disk space) and so the original files are needed to recreate the thumbnails when the cache directory is emptied.

  • I must notice that a "crop only" cropping mode is missing. We have 3 resizing modes (0, 2, 3) and only one that crops with resizing (1). It is impossible, however, to zoom without resizing...

  • Is there a parameter to prevent images scaling up if the proportions are lower than those sent to the script?

    • Solved this myself, below...

      // Get original width and height
      $width = imagesx ($image);
      $height = imagesy ($image);
      $origin_x = 0;
      $origin_y = 0;

      // generate new w/h if not provided
      if ($new_width && !$new_height) {
      $new_height = floor ($height * ($new_width / $width));
      } else if ($new_height && !$new_width) {
      $new_width = floor ($width * ($new_height / $height));
      }

      ... I added ...

      // prevent scale up
      if ( $width < $new_width && $height < $new_height ) {
      $new_width = $width;
      $new_height = $height;
      }

      • Profile

        I decided not to implement this in TimThumb itself because TimThumb is meant to make the images the size you specify regardless of how the images are scaled, and this would not do that - however your fix looks like it would work in the same way my own one would.

  • On your demo timthumb has white borders, when I have come to use this on my site the "borders" appear black? Is there any way round this or am I missing something obvious?
    Really need a solution as I was planning on using timthumb for this.

    many thanks

  • Hi there, thanks for the completely brilliant timthumb. It is quite simply awesome.

    You may already know this, but I discovered today that images with a + in the filename will cause timthumb to break. Not a real problem for me, I am simply working around it by telling the client to not use + signs in filenames, but I thought you'd like to know.

  • Hi Ben,

    I have a problem with timthum script (so it seems) and the new version of wordpress (3.3.1) Before the wp upgrade images on my website were displaying normally but now the thumbs are streched. I have tried everything possible (file/folder permisions, cache/temp folders, reinstaling theme, tried on a fresh wp install) but no solution. I am using awake 2.5 theme by http://www.mysitemyway.com (they cant solve it either) and like said wp 3.3.1.

    Uncle google did not give any solution even after long hours of searching...

    Here is an example of distorted thumbs: blog

    Will really appreciate help!

Leave a Reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About me

About BenMy name is Ben Gillbanks. I'm a lover of Video Games, WordPress, Web Development and everything in between.

I have been working on the internet since 1998, and working with computers even longer. I am a hardcore Nintendo fanboy and have owned most of their consoles at one stage or another.

Read more about me on my about page.


Follow Me


Random Link-outs

  • WPVote WordPress voting and article submission community website
  • Front Row Reviews News and Reviews for the latest movies
  • Totebo Flash games with monkeys
  • CodeAid Web development hints and tips
The Binary Network links to all my websites
bengillbanks.co.uk - Ben Gillbanks

Ben Gillbanks

All my websites under 1 roof, the easiest way to find out what I do

Pro Theme Design - premium WordPress themes

Pro Theme Design

Premium WordPress themes by web design pros (erm... that includes me)

Binary Joy - gaming news and reviews

Binary Joy

Gaming news and reviews

Binary Sun - play free online games

Binary Sun

Play and download free and paid games (many made by me)

Gaming Angel - download and play games online

Gaming Angel

Stacks of shareware games, free to try and cheap to buy