Home > PHP, Wordpress > Howto: Create custom thumbnails from Flutter image fields

Howto: Create custom thumbnails from Flutter image fields

If you’ve worked with Flutter / Fresh pages (wordpress plugin) before, you’ll know that you can use the supplied get_image() function to insert the relevant image field into your template. The problem however is that these images are generated automatically during the upload process and thus conform to the whatever dimensions are set for that field.

There are instances however where you’d need to display the same image but at different sizes, for example a widget vs ‘more info’ display. This is when the following function becomes quite handy.

The auto thumbnail function

You can insert this function straight into your functions.php, a usage example will follow below.

function jt_get_thumb($fieldName, $groupIndex=1, $fieldIndex=1, $readyForEIP=true, $params, $returnimg=true){
        // use flutter's default function to get our field value (non thumbnailed image).
	$file = get($fieldName, $groupIndex, $fieldIndex, $readyForEIP);
        // if the field's empty, we can't continue, return an empty string
        if (empty($file)){
            return "";
        // we want the base filename
	$file = str_replace(FLUTTER_FILES_URI, "", $file);
        // get the current image dimensions
	$jtSize = getimagesize(FLUTTER_FILES_PATH.$file);
        // break our params up into a useable array.
        $params = explode("&",$params);
	$i = 0;
	foreach($params as $p){
		$tmp = explode("=", $p);
		$jtParams[$tmp[0]] = $tmp[1]; 
       // no point in continuing if the width or height params aren't set, or the original image's dimensions are smaller than the supplied params
	if ((!isset($jtParams["w"]) || !isset($jtParams["h"])) || ($jtSize[0] <= $jtParams["w"] && $jtSize[1] <= $jtParams["h"])){
		return ($returnimg) ? "<img src='". FLUTTER_FILES_URI.$file ."' />" : FLUTTER_FILES_URI.$file;
	//generate thumb using flutter's default method (phpThumb class)	
	include_once(FLUTTER_PATH . "/thirdparty/phpthumb/phpthumb.class.php");
	$phpThumb = new phpThumb();
	$md5_params = md5($file . $params);
	$create_md5_filename = 'jt_th_'.$md5_params."_".$file;
	$output_filename = FLUTTER_FILES_PATH.$create_md5_filename;
	$final_filename = FLUTTER_FILES_URI.$create_md5_filename;

// experimental, uncomment to test : check if the thumbnail has already been created and use that instead	
//	if (file_exists($output_filename)){
//		$attr_params = "style='width:".$jtParams['w']."px;  height:".$jtParams['h']."px;'";
//		return ($returnimg) ? "<img src='$final_filename' $attr_params />" : $final_filename;
//	}
	foreach($jtParams as $key => $val){
		$phpThumb->setParameter($key, $val);
	if ($phpThumb->GenerateThumbnail()) {
		if ($phpThumb->RenderToFile($output_filename)) {
			$file = $final_filename;
		$attr_params = "";
	} else {
                // if the thumbnail generator fails for some reason, rather return the original image with a forced style attribute
		$file = FLUTTER_FILES_URI.$file;
		$attr_params = "style='width:".$jtParams['w']."px;  height:".$jtParams['h']."px;'";
	return ($returnimg) ? "<img src='$file' $attr_params />" : $file;


Pretty straightforward, the function accepts the standard flutter get() parameters with 2 extras added on to the end. The last two parameters are as follows :

  • $params (string): a standard query string containing the various parameters for the thumbnail generator. Example – “w=400&h=200&zc=1”. The available parameters conform to flutter’s default image thumbnail parameters, see flutter’s documentation for a full reference.
  • $returnimg (bool, default true): set to true to return a formatted img tag, whereas false will return just the full URI based path to the image.

Example Useage

// will output full img tag
echo jt_get_thumb("news_img", 1, 1, false, "w=250&h=250&zc=1");

// build the image string up manually
echo "<img src='" . jt_get_thumb("news_img",1,1,false,"w=250&h=250&zc=1",false) . "' class='newsImgClass' />";

Naturally, this function assumes that the field being queried points to a valid image file, you could of course flesh it out with some fail-safe checks if you’re that way inclined. Please feel free to post any improvements in the comments and I’ll see about adding them to the next version for everyone to use.

Categories: PHP, Wordpress
  1. October 29, 2009 at 4:40 pm

    Great Post! Worked beautifully for me – Thx!

  2. December 4, 2009 at 7:10 pm

    Worked brilliantly thank you very much!

  3. S3nd41
    May 10, 2011 at 10:05 pm

    Hello, I tried enclosing your code in “” but it bugged the wp-admin login screen. It isn’t displaying anymore. Please post your code exactly at should be pasted so I can fix this. Thanks!

    • S3nd41
      May 10, 2011 at 10:07 pm

      Enclosing it with .

  4. S3nd41
    May 10, 2011 at 10:08 pm

    Can’t post the php opening and closing tag. You got the picture.

  5. S3nd41
    May 10, 2011 at 10:09 pm

    I got it, thnx!

  1. October 8, 2009 at 11:07 am
  2. December 19, 2011 at 12:37 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: