Archive

Archive for the ‘Wordpress’ Category

Howto: Create custom thumbnails from Flutter image fields

September 24, 2009 8 comments

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]; 
		$i++;
	}
        
       // 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();
	$phpThumb->setSourceFilename(FLUTTER_FILES_PATH.$file);
	$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.

Advertisements
Categories: PHP, Wordpress

Simplifying Flutter duplicate groups and fields

September 21, 2009 11 comments

Flutter or Fresh-pages is a really handy plugin for WordPress. With it, you can quite easily create custom write panels, fields and groups of information.

Having worked with this plugin quite extensively recently, I have encountered a few issues which have proved somewhat frustrating, dealing with duplicate groups or fields in particular. Flutter uses a counter based system in which to access duplicate groups. Simple enough when using a standard for loop and all the duplicates are in the correct order – but somewhat tricky to handle when say, deleting one of the duplicates.

I’ve since resorted to writing a function to make handling these duplicates and a number of associated problems whilst looping through them much, much simpler.

Which brings us to the following function – which you can paste straight into your theme’s functions.php file :

function jt_get_flutter_duplicates($field, $group){
	if (is_string($group)){
		$numfiles = getGroupDuplicates($group);
		$isgroup = true;
	} else {
    	$numfiles = getFieldDuplicates($field, $group);
    	$isgroup = false;
	}
    $return = false;
    if ($numfiles > 0){
        $return = array();
        $count = 1;
        $tmp = "";
        $first = "";
        $total = ($numfiles > 1) ? 100 : 1;
        while($count <= $total){
        	if ($isgroup){
        		$value = get($field, $count, 1);
        	} else {
            	$value = get($field, $group, $count);
        	}
            if (empty($value) || $value == $first || $tmp == $value){
                $count++;
                continue;
            } elseif ($count == 1){
                $first = $value;
            }

            $tmp = $value;
            $return[] = ($isgroup) ? $count : $value;
        }
    }
    return (is_array($return) && count($return) > 0) ? $return : false;
}

I’m not going to go into too much detail right now, but essentially what this function does is gets the required field or group duplicates, loops through them (up to a max of 100 iterations just to be safe) and places them into an array containing the correct index values (if a group) or an array of field values (if a field).

Group duplicates

Set the first parameter to the name of a field within the group which should contain a unique value (such as name, or caption). The second parameter can be any field within the required group.

$groupItems = jt_get_flutter_duplicates("news_image", "news_image");
if (is_array($groupItems) && count($groupItems) > 0){
    foreach($groupItems as $i) {        
	echo get_image("news_image",$i, 1, 1);
        echo "<br />" . get("news_image_caption",$i, 1, false);
    }
}

Field Duplicates

Set the first parameter to the name of the field you want to retrieve. Set the second parameter to the group index you want to retrieve the field duplicates from. For instance, you could have 1 group which can be duplicated, containing 3 fields, one of which can be duplicated. When looping through the group duplicates, you’ll want another sub-loop to handle the duplicated field contents. The function will return an array of values which you can then use in a standard foreach loop.

$groupItems = jt_get_flutter_duplicates("office_title", "office_title");
if (is_array($groupItems) && count($groupItems) > 0){
    foreach($groupItems as $i) {  
        echo get("office_title",$i,1,false);
        $contacts = jt_get_flutter_duplicates("office_contact", $i);
        if (is_array($contacts) && count($contacts) > 0) {           
           foreach($contacts as $contact){               
               echo "<br />Contact Person: " . $contact;
           }
        }
    }
}

This is of course a quick fix and by no means infallible, so I’d love to hear if you have any improvements or a simpler way of dealing with this particular issue.

Categories: PHP, Wordpress