Skip to main content

Resize Image Dynamically in PHP with TimThumb

PHP TimThumb

TimThumb is simple, flexible yet powerful php script that resizes images dynamically. Every website or blog uses thumbnail images, it’s a very useful part of the website.If you don’t see any images or thumbnail you quickly skip the website or article. You wont read it in deep but if you find few thumbnails between or in start of the News or article you would like to read and stay more on the page or article. It’s human nature visual images attracts mind rather than just bunch of words, isn’t it?

TimThumb is very light waited php script that resize images by passing bunch of parameters in specific manner. TimThumb was originally developed by Tim, a friend of Darren Hoyts, who created the original script. Since it took over by Binary Moon who letter works on bug fixes and did lots of addition.

For TimThumb to work you just need to include it in your website or your code and pass the required parameters let me show you with an example.

First download TimThumb library you can do so from https://github.com/joshihardik/timthumb

and add timthumb.php to your project or website.

Suppose you have uploaded it under your root/scripts/timthumb.php now just call TimThumb with appropriate parmaeters

<img src="/scripts/timthumb.php?src=/yourimageurl/image.png&w=50&h=50" alt="resized image by 50X50" />

Above example will resize image to 50X50

TimThumb Parameter List

stands for values What it does
src source url to image Tells TimThumb which image to resize › TimThumb basic properties tutorial
w width the width to resize to Remove the width to scale proportionally (will then need the height) › TimThumb width tutorial
h height the height to resize to Remove the height to scale proportionally (will then need the width) › TimThumb height tutorial
q quality 0 – 100 Compression quality. The higher the number the nicer the image will look. I wouldn’t recommend going any higher than about 95 else the image will get too large › TimThumb image quality tutorial
a alignment c, t, l, r, b, tl, tr, bl, br Crop alignment. c = center, t = top, b = bottom, r = right, l = left. The positions can be joined to create diagonal positions › TimThumb crop position tutorial
zc zoom / crop 0, 1, 2, 3 Change the cropping and scaling settings › TimThumb crop scaling tutorial
f filters too many to mention Let’s you apply image filters to change the resized picture. For instance you can change brightness/ contrast or even blur the image › TimThumb image filter tutorial
s sharpen Apply a sharpen filter to the image, makes scaled down images look a little crisper › tutorial
cc canvas colour hexadecimal colour value (#ffffff) Change background colour. Most used when changing the zoom and crop settings, which in turn can add borders to the image.
ct canvas transparency true (1) Use transparency and ignore background colour

More Examples

1.
Basic example with minimum parameters. Automatically resizes to 100 x 100
2.
High quality = awesome image
3.
Low Quality = rubbish looking image
4.
Specify the width only and the height will change to be relatively sized to keep the picture in proportion
5.
Specify the height only and the width will change to be relatively sized to keep the picture in proportion
6.
Width and Height together
7.
Width and Height again, showing that any image ratio can be used

 

Conclusion

TimThumb is a small php script for cropping, zooming and re-sizing web images (jpg, png, gif). Perfect for use on blogs and other applications. However TimThumb is no longer supported or maintained by its developer as TimThumb has a major security exploit found and it hurts lots of website. In result They dropping all future support and maintenance for TimThumb but I will still suggest to give a try to TimThumb but don’t use it with WordPress. you can find the major issues at https://www.binarymoon.co.uk/2014/07/dont-use-timthumb-instead/

Hardik Joshi

Hardik Joshi is Founder, Developer and Writer at eCodezone.com, He loves to write usable code for the Web and other digital sources.