Timthumb Usage and Troubleshooting – Create Image Thumb and Crop

by Hiroshi on January 18, 2013

in Image Handling, Thumbnail

I have been using thumbnail class by ‘Ian Selby’ v1 that can crop image from center and create thumbnail by given width and height parameters but that was not flexible. If the image has same width and height ratio (e.g. 100×100 or 500×500) then it is great to crop and resize image from center and the full image thumbnail will be displayed as a result.

If this is not the case and width of image is like 200px and height is 800px then that class is not good enough. It will not crop full image but it will crop image from center. I needed a thumbnail class that can create thumbnail of full image, doesn’t matter what is the width and what is the height of an image. Recently I used Timthumb that worked great. Here is how to use Timthumb php script to crop image and create thumbnail.

You will need Timthumb script but after you download/copy it, make sure it has php closing tag at the end of it. If it does not have php closing tag, add closing tag to it. Also see here and here.

Ok Timthumb is said to be vulnerable. In my opinion, it is said because it requires cache folder with chmod 777 to use image cache. The point is, if you are security conscious (which you should be), do not use cache for this script. Remove cache folder and turn off cache from the script.

These two lines below

if(! defined('FILE_CACHE_ENABLED') ) define ('FILE_CACHE_ENABLED', TRUE);
if(! defined('FILE_CACHE_DIRECTORY') ) define ('FILE_CACHE_DIRECTORY', './cache');

Change these in script to these

if(! defined('FILE_CACHE_ENABLED') ) define ('FILE_CACHE_ENABLED', FALSE);
if(! defined('FILE_CACHE_DIRECTORY') ) define ('FILE_CACHE_DIRECTORY', '');

And you are good to go secure. At least in my opinion.

Ok. This script will create thumbnails, that’s for sure. If its not, then you need to troubleshoot your code.

Always confirm that the image URL that you are giving is correct?

If you are using mod-rewrite, URL rewriting, then make sure to provide absolute path of image.

I repeat: Use absolute path, for script and for image.

If images are not displaying, it means there is some problem in timthumb file path or the image path.

And of course you need GD2 library, that is enabled in most cases (on hosts / servers). If using localhost (wamp etc.), edit configfiles > php.ini and make sure GD2 extension is uncommented.

;extension=php_gd2.dll
to
extension=php_gd2.dll

That’s it.

Use firebug to see your output of image and track the path of image and script if you are not getting the thumbnails.

While the requirement is only this:

<img src="timthumb.php?src=image.jpg&zc=1&w=80" />

In my case I had to use it like below:

<img src="<?php print $websiteURL; ?>timthumb.php?src=<?php print $websiteURL; ?>images/products/<?php print mysql_result($checkCRf,0+$i,"file"); ?>&zc=1&w=80" width="80" />

means this

<img src=" absoulte-path-of- timthumb.php?src=absolute-path-of- image.jpg&zc=1&w=80" />

I hope I made sense above.

Related Posts

Previous post:

Next post: