For those who don’t know what timthumb are. Check out the most popular image crop PHP used in WordPress.

TimThumb was no doubt used by many popular free themes and premium themes but in some cases, timthumb did not work in your multisite and network site.

Here’s some step to make timthumb work with a single site and network multisite WordPress installation.

First – create a timthumb-config.php in the same directory of timthumb.php

Try not to edit the timthumb.php so updates on the script can be consistent without you having to edit the code again when new patch or update to timthumb released.

in timthumb-config.php add this code

[php]
<?php
if( is_file( ‘../../../../../wp-config.php’ ) ) {
require_once(‘../../../../../wp-config.php’);
} else {
require_once(‘../../../../wp-config.php’);
}
define (‘ALLOW_EXTERNAL’, FALSE);
$tim_uploads = wp_upload_dir();
$tim_upload_path = $tim_uploads[‘basedir’] . ‘/cache/’;
$tim_upload_path_check = $tim_uploads[‘basedir’] . ‘/cache’;
//Create the upload directory with the right permissions if it doesn’t exist
if( !is_dir( $tim_upload_path_check ) ){
mkdir($tim_upload_path_check, 0777);
chmod($tim_upload_path_check, 0777);
}
define ( ‘FILE_CACHE_DIRECTORY’, $tim_upload_path_check );
?>[/php]

This will allowed timthumb to create a cache directory in wp-content/uploads for a single site and wp-content/blogs.dir/BLOG_ID/files in multisite. This way all the cache/temp files will not be saved inside the themes directory.

Second – create a function to fetch the images

Please note: in this case, my timthumb.php were inside lib/timthumb/timthumb.php however your timthumb location and default images may be different from the functions code.

copy-paste this code to your functions.php

[php]
if( !function_exists( ‘get_featured_post_image’ )):
function get_featured_post_image($width, $height, $class, $title) {
global $blog_id,$wpdb, $post, $posts;
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,’large’);
$image_url = $image_url[0];

if(!$image_url){

$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘/<img.+src=[\’"]([^\’"]+)[\’"].*>/i’, $post->post_content, $matches);
$first_img = $matches[1][0]; ?>

<?php if($first_img) {

if( !is_multisite() ) {
return "<img class=’" . $class . "’ src=’" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . $first_img . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl’ alt=” title=’" . $title . "’ />";
} else {
return "<img class=’" . $class . "’ src=’" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . get_current_site(1)->path . str_replace( get_blog_option( $blog_id,’fileupload_url’),get_blog_option($blog_id,’upload_path’), $first_img) . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl’ alt=” title=’" . $title . "’ />";
}
} else {
return "<img class=’" . $class . "’ src=’" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . get_template_directory_uri() . ‘/lib/styles/images/feat-default.jpg’ . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl’ alt=” title=’" . $title . "’ />";
}

} else {

if( !is_multisite() ) {
return "<img class=’" . $class . "’ src=’" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . $image_url . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl’ alt=” title=’" . $title . "’ />";
} else {
return "<img class=’" . $class . "’ src=’" . get_template_directory_uri() . "/lib/timthumb/timthumb.php?src=" . get_current_site(1)->path . str_replace( get_blog_option( $blog_id,’fileupload_url’),get_blog_option($blog_id,’upload_path’), $image_url) . "&amp;h=" . $height . "&amp;w=" . $width . "&amp;zc=1&amp;a=tl’ alt=” title=’" . $title . "’ />";
}
}
}
endif;
[/php]

Parameter Explanation

[php]
$width = desired width for timthumb to cropped
$height = desired height for timthumb to cropped
$class = add class to the image – alignleft, aligncenter or any custom class
$title = for title
[/php]

Timthumb will first check if the post had WordPress post thumbnail exist, if not the function will regex or get the first internal images from the post *external image will not be fetched if both post thumbnail and get first images failed, default images will be used instead.

Third – how to use the new function in the template

just add this code to any location you want the featured timthumb cropped image to show.

only cropped image

[php]<?php echo get_featured_post_image(150, 150, ‘alignleft’, get_the_title()); ?>[/php]

cropped image with a link

[php]<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_featured_post_image(150, 150, ‘alignleft’, get_the_title()); ?>
</a>[/php]

that’s all folks, let me know if any improvement to the code can be done. Codewise is always welcome.

Thanks