in

Guide to Moving Away from TimThumb Script to BFIThumb

Timthumb developer has been closed, and that script is not continue because its script contains many bugs. Where, through its bug, someone can inject script in your hosting.

And now, its time to move from timthumb to another on-the-fly image resizeing script. In wordpress, many developers offers image resizing technique, such as Matthew Ruddy who creating WordPress Timthumb alternative, you can download its script from github

Then, i am choosing use BFIThumb script. Its not a plugin, but litle library that support wordpress image class, or in simple word : its enhancing wordpress image class in order you can easily got these features :

  1. Grayscale
  2. Color (colorize)
  3. Opacity
  4. Negate
  5. Resize images down or UP
  6. Image Quality (1-100)

All feature above is working on the fly. Cool, right?

So, after download BFIThumb, its time to create a custom function to display thumbnail easily in front-end (template). Open your functions.php file, and paste this code

require_once('BFI_Thumb.php');

function first_image($w,$h,$o,$gray,$crop) {
  global $post, $posts;
 $first_img = '';
 ob_start();
 ob_end_clean();
 $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
 $first_img = $matches [1] [0];

 if(empty($first_img)){ 
 $img_dir = get_template_directory_uri();
 $first_img = $img_dir . '/images/thumb.gif';
 }
 //return $first_img;
 $params = array( 'width' => $w, 'height' => $h, 'opacity' => $o, 'grayscale' => $gray, 'crop' => $crop );
 $image=bfi_thumb($first_img,$params);
 return $image;
}

How the function is work?

  1. We’are using a regular expression to get img url in a post.
  2. Then, when the script got it, BFIThumb will resize it or anything what you want.
  3. This function return image url in resized version.
  4. This function support many parameter, or you can modify it as you like.

Use it on template

To use this function, just call it on template where ever you want.

<img src="<?php echo first_image();?>" alt="" />

I hope this article will guide yo to move away from timthumb, because many site has been hacked which still using timthumb.

What do you think?

0 points
Upvote Downvote

Written by Bimo Hery Prabowo

Web developer, big fan of CakePHP, loving wordpress soo much. You can found him on Google+ and Facebook. Dont forget to follow him on Twitter.

One Comment

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.