Monday 29 April 2013

My Wordpress Theme has a TimThumb script that i can't get to work

My Wordpress Theme has a TimThumb script that i can't get to work - Now What !! Having a look around the web it seems I'm not the only one that seems to have been struggling with getting the TimThumb script to work. Tim Thumb Issue Well here's my hack for a theme when all was lost. This is the code I had in the theme that called up the Tim Thumb script. Here's the call up from the template.
echo themex_thumbnail($post->ID,738);
And then the function in the templates functions.php
function themex_thumbnail($ID, $w = 0, $h = 0){
 global $blog_id;
 $src = wp_get_attachment_url( get_post_thumbnail_id($ID) );

 if (isset($blog_id) && $blog_id > 0) {
  $imageParts = explode('/files/', $src);
  if (isset($imageParts[1])) {
   $src = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
  }
 }

 $url = THEMEX_URI.'extensions/timthumb/timthumb.php?src='.urlencode($src);
 if($w = intval($w)){
  if($h!=0 && $h=intval($h)) { 
   $url.='&w='.$w.'&h='.$h.'&a=t';
  } else {
   $url.='&w='.$w;
  }
 }
 return $url;
}
Here's the call up I've replaced with
echo lrip_gallery_thumbnail($post->ID,'blog');
And the following function. You can add all your different sizes to this list, and change the 'blog' call up; for example 'small' !
unction lrip_gallery_thumbnail($ID,$size){  
 global $blog_id;
 $src = wp_get_attachment_url( get_post_thumbnail_id($ID) );
// going to explode using .jpg and fix on a new ending 
 $img_url_array = explode('.jpg', $src);
 if ($size=='small'){
 $img_url = $img_url_array[0].'-150x150.jpg';
 } elseif($size=='blog'){
 $img_url = $img_url_array[0].'-738x415.jpg';
 }else {
 $img_url = $src;
 }
 return $img_url; 
} 
As well as making a copy at the right size and with '-738x415' add to the end of the filename; we also need to make it so the Wordpress automatically makes additional sized images. Its pretty simple all you need to do is add this to the main function.php

add_image_size( '738x415', 738, 415, true );
the first part is the name reference. It'll stick the size on the end anyway. And then comes the sizes and at the end the 'true' tells us that the image will be cropped to these sizes. And that's it.

No comments: