Monday, March 12, 2012

Wordpress - How to replace TimThumb Improved

Back in 2009 I replaced TimThumb in the Mimbo WordPress Template with a change in the function that was used to get the thumbnail images. This function is found under "Theme Functions" and it is called "Get_post_image". The problem was that TimThumb was slow, and there already is a thumbnail image created whenever you upload an image to your Wordpress blog. That created thumbnail image usually ends in "-150x150.jpg".

The version of this function that I wrote back in 2009 was too slow because it used the "if file exists" test to see if the image name that it created was correct and if it did not exist then it went with the full sized image and just scaled it down by specifying a width and height of 150 and 150.

So the simplest way to select the already available thumbnail image is to remove the last 12 digits and replace them with "-150x150.jpg", Right? Unfortunately reality varies greatly from theory. The first problem is that you might have a ".gif" or some other image extension. So you have to remove the extension, then remove the next few digits, then replace them and then put the extension back.

Well that works about 75% of the time. The next issue that you have to deal with is if the image is smaller than around 600 pixels wide, there might not be any extension added onto the image that needs to be removed, so you just need to add the "-150x150" to the image. How can you tell what format the image is in? That is done by breaking down the image and then looking for first a "-", then a 3 digit number, then an "x" then, optionally, a second three digit number. So here is the code to do all of that:




/* For Getting the Image FASTER */
//GET-POST-IMAGE1 by Bob Davis - improved version of script by Tim McDaniels

function get_post_image1 ($post_id=0, $width=0, $height=0, $img_script='') {
  global $wpdb;
  if($post_id > 0) {
    $sql = 'SELECT post_content FROM ' . $wpdb->posts . ' WHERE id = ' . $wpdb->escape($post_id);
    $row = $wpdb->get_row($sql);
    $the_content = $row->post_content;
    if(strlen($the_content)) {
      preg_match("/<img src\=('|\")(.*)('|\") .*( |)\/>/", $the_content, $matches);
      if(!$matches) {
    preg_match("/<img class\=\".*\" title\=\".*\" src\=('|\")(.*)('|\") .*( |)\/>/U", $the_content, $matches); }
    $the_image_ext = substr($matches[2],-4);
    if ($the_image_ext == ".JPG") {  $the_image_ext = ".jpg"; }
    $the_image_dash = substr($matches[2],-12,-11);
    $the_image_num = substr($matches[2],-11,-8);
    $the_image_x = substr($matches[2],-8,-7);
// replace -???x??? with -150x150 and then add .jpg back on
    $the_image_src = substr($matches[2],0,-4);
    $the_image_repl = $the_image_src . '-150x150' . $the_image_ext;
// just add -150x150 and then add .jpg back on
    $the_image_src = substr($matches[2],0,-12);
    $the_image_add = $the_image_src . '-150x150' . $the_image_ext;
// Check to see it the name ends in -???x???
        if(($the_image_dash=='-') && (is_numeric($the_image_num)) && ($the_image_x=='x')) {
       $the_image = '<img alt="" src="' . $the_image_add .'" width="' . $width . '" height="' . $height . '" />';
           } else {
           $the_image = '<img alt="" src="' . $the_image_repl . '" width="' . $width . '" height="' . $height . '" />';  
                 }
        return $the_image;
      }
    }

  }
Post a Comment