7 TimThumb resizer solutions



Have you heard of timThumb lately? Well this thing is not so quite new but its already famous in most recent modern themes of wordpress. For those who are new with wordpress, you might be wondering whats the use of it and some may even don’t know what the heck it is.
Thats why, i decided to post this and list out the possible solutions to the timthumb issues that i have experienced before.

What is timthumb?
In the layman terms, timthumb is one of a tool that has been designed to ease bloggers in order to insert the picture especially the thumbnails image.For efficient bloggers, you might want to easily put in your images without entangled with the html image code tag.Refer to the images below to know the differences with and without using timthumb.

In advance terms, timthumb or timthumb.php script is a custom image-sizing script, that allows you to produce a cropped and sized version of an image.The first developmnet is for MimboPro and Darren Hyot is the creator of this hot add on.
Before i go further with the usual problems faces of using this timthumb.php, i would like to list out some of the advantages of using this great script.

Advantages:

  1. 1. Efficient to use for bloggers who wants easy method of puttin on your images in blog.(no entangle with the hmtl code tag anymore)
  2. 2. Make the cropping, resizing and editing the images more easier as you can determine the sizes.
  3. 3. Its useful to be used together with lightbox plugins.

For the disadvantages of this timthumb you need to make sure after the installation of this script, the permission of cache folder is permitted.The script used a GD library to work on and needs some of folders to be CHMOD.I will explain in details in the solutions below.

Possible solutions of timthumb issue
After installation of this script you might have the 500 Internal Server Error or even a broken linjks of some of your images.If you experienced this, don’t be panic because you are not alone.Me myself have personally experienced this kind of problems.
In order to overcome this problem, what i did was

First: Check your permission CHMOD of related folders

  • • Change the cache folders to 755
  • • Change the folders that contain your images to 755 and
  • • make sure your wp-content set to 755

You may find some of the solutions given on the net advising you to change the mode to 777 but i personally wont recommend it as it may jeopardise your ownership.

Ok after done this, i still have the internal server error so i try the second solution.

Second:Checking your server have GD library(basic requirements)
To check you might want to check the php version provided by your hosting provider.Usually if you have the latest version of php or php4.3+ the gd library is already come in package.But if it doesnt you may want to ask your host provider to upgrade it to the latest version.

Third:Ob-clean method
After done checking my host it seems i got the latest version of php and i start wondering, should i use this timthumb.Well there must be something that i left while installing.
So i figure it out in my findings of using ob-clean script.

Using the “ob_clean” script is to clean the previous buffer bytes for those consist warning messages, so that it only start to do buffering for those image bytes that you want to send out.A lilttle modification is need in the timthumb.php file as below:

PHP code:

function show_cache_file ($cache_dir, $mime_type) {
    $cache_file = $cache_dir . '/' . get_cache_file();

    if (file_exists($cache_file)) {

        $gmdate_mod = gmdate("D, d M Y H:i:s", filemtime($cache_file));

        if(! strstr($gmdate_mod, "GMT")) {
            $gmdate_mod .= " GMT";
        }

        if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {

            // check for updates
            $if_modified_since = preg_replace ("/;.*$/", "", $_SERVER["HTTP_IF_MODIFIED_SINCE"]);

            if ($if_modified_since == $gmdate_mod) {
                header("HTTP/1.1 304 Not Modified");
                die();
            }

        }

        $fileSize = filesize ($cache_file);
        $buffer = file_get_contents($cache_file);

        ob_get_clean();
        ob_start();

        header ('Last-Modified: ' . $gmdate_mod);
        header ('Cache-Control: max-age=9999, must-revalidate');
        header ('Expires: ' . $gmdate_mod);
        header ('Accept-Ranges: bytes');
        header ('Content-Type: ' . $mime_type);
        header ('Content-Length: ' . $fileSize);

        echo $buffer;
        $sContents = ob_get_contents();
        ob_end_clean();
        echo $sContents;
}

Note that the modified code is at the “show_cache_file”.Please backup your timthumb.php before proceed with this modification in case of anything.

Fourth:Checking the array path
Unfortunately, the third solution above still wont works for me so i decided to dig more on how to overcome this.Then, i checked the array path for the files as my problems now have turn out to be broken images link.The internal errors already gone but the images is broken, so i thought it would be something related with the link path of the thumb.

To check the array i look at the function get_document_root

PHP code:

function get_document_root ($src) {
	if( @file_exists( $_SERVER['DOCUMENT_ROOT'] . '/' . $src ) ) {
		return $_SERVER['DOCUMENT_ROOT'];
	}
	// the relative paths below are useful if timthumb is moved outside of document root
	// specifically if installed in wordpress themes like mimbo pro:
	// /wp-content/themes/mimbopro/scripts/timthumb.php
	$paths = array( '..', '../..', '../../..','../../../..','../../../../..','../../../../../..');
	foreach( $paths as $path ) {
		if( @file_exists( $path . '/' . $src ) ) {
			return $path;
		}
	}

}

It may defer with yours depending on the themes you are using.
The $paths=array(‘..’,) should be checked according to your folder themes path if you put it outside the document root.
You need also to check the Use of absolute paths for your images (incl. http://…). Make sure you did not use the external images (for security reasons).
After done checking,still i have the broken image link.So i try the next solution.

Fifth:Using the file_get_contents function
Maybe the ob_get_clean wont works for me(or cache files of mine is somehow wont read properly) so i decided to try the file get contents function.

PHP code:

$fileSize = filesize( $cache_file );

header( "Content-Type: " . $mime_type );
header( "Accept-Ranges: bytes" );
header( "Last-Modified: " . gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) ) . " GMT" );
header( "Content-Length: " . $fileSize );
header( "Cache-Control: max-age=9999, must-revalidate" );
header( "Etag: " . md5($fileSize . $gmdate_mod) );
header( "Expires: " . gmdate( "D, d M Y H:i:s", time() + 9999 ) . "GMT" );

$img_data = file_get_contents( $cache_file );

echo $img_data;

exit;

In this alternative, you are changing your read file function as there are other alternatives that you may accross like the combination between fopen and fpassthru but it only works on some server so the best bet is using the file get contents to recall back the functions to work with the cache file.

Sixth:Checking your .htaccess file
With no avail my problems for timthumb still not solved. So i keep searching for the solutions and found out that it might be my .htaccess file corrupted.For whatever reason, the .htaccess file can become corrupted perhaps due to a plugin however many things could cause it. So what i did is backup my current .htaccess file and uploading a blank .htaccess file and set the permalink structure again. Toinkz! it still wont works so i restore back my old .htaccess file back again.

After i’m about to given it all up, i decided to try the last solution of contacting my hosting provider. The only reason i got into this problem is because i’m moving into a new hosting so i might not be aware of some new features of the new hosting. To contact the hoster is my last resort after ive done some alternative solutions about this issue.

Seventh:Consult your hosting provider
After contacting my hoster, pooff the issue is solved! Yayy!!Then i realised that the new hosting is using the suPHP.

A couple of lessons learned here while searching for the timthumb solutions and before you panic about what had happened to your blog if the Internal Server Error occur you may consider the above solutions listed. Hope it helps for those who experienced this kind of problems.
But if you had another solutions and want to correct me or anything just drop by and leave your comments here so we can share it together.

You can also visit Darren Hoyts TimThumb support forum for further assistance.



6 Responses to “7 TimThumb resizer solutions”

  1. mhmdtq says:

    thank you sista skulblakashven :)

  2. Xiao Bayon says:

    I actually made a little video about this. Would you consider checking it out and maybe leave a comment on it? (I put the link in the “website” field hopefully you can access it. thanks!

  3. pausleal says:

    Keep posting stuff like this i really like it

  4. sgn says:

    Great site. A lot of useful information here. I’m sending it to some friends!

Leave a Reply