Saturday, August 13, 2011

PHP code for rendering a 'barrel widget' for charting and graphing.

I had call to write a PHP based barrel widget the other day and through I'd share the process.
The end-result needed to look like this: 
 
In order to achieve this, I chopped the graphic into the following PNG files:






Then the code which made it render as a complete new 'barrell chart' or 'barrell widget' looked like this:

<?php
function graph_render_barrel($val,$name){
    //$val is the percent value passed to the function, and $name is the name of the file to be saved
    $filename=$name;
    $img = imagecreatefrompng("g_barrel.png");
    imagealphablending($img, true);
    imagesavealpha($img, true);
    $greenbase = imagecreatefrompng("g_barrel_breen_bot.png");
    imagealphablending($greenbase, true);
    imagesavealpha($greenbase, true);
    $greenmid = imagecreatefrompng("g_barrel_breen_bak.png");
    imagealphablending($greenmid, true);
    imagesavealpha($greenmid, true);
    $greentop = imagecreatefrompng("g_barrel_breen_top.png");
    imagealphablending($greentop, true);
    imagesavealpha($greentop, true);
    $stretch=floor(184*($val/100)); //0 to 184
    imagecopyresampled($img, $greenbase, 2, 211, 0, 0, 143,21, 143,21);
    imagecopyresampled($img, $greenmid, 2, (211-$stretch), 0, 0, 143,(20+($stretch-20)), 143,20);
    imagecopyresampled($img, $greentop, 2, ((211-$stretch)-22), 0, 0, 143,45, 143,45);
    imagepng ( $img , "portal/saasmsallstar/UserFiles/bb_crp_graph_images/$filename" , 0 ,  PNG_ALL_FILTERS);
    imagedestroy($img);
    return($name); //handle possible retuns here
}
?>

So, in practice it could be used like this:

<?php
echo("<img src=\"".graph_render_barrel(66,"path/to/new_graphic.png")."\" >");
?>

Managing Timezones in BlueBox2.0

If you need to set the timzone for your BB2.0 system, or for specific timezones for individual users, you can use the following built-in settings:
  • for a system-wide setting, set the config (admin->config) setting system_default_timezone
  • for a user-specific timezone setting there is a field in the bb_users table called 'timezone'
The value for either of these can be set to one of the standards described in http://www.php.net/manual/en/timezones.php

Adding Multiple email responses to new data added in BB2.0

If you are familiar with the bbsetting_email_onadd setting for BlueBox2.0, you may be interested to know that you can set multiple arrays which send different results to various recipients, as follows:

In a .php file, use the following:

var $bbsetting_email_onadd=array(
array("to"=>"some@user.com","bcc"=>"","
from"=>"admin@domain.com","subject"=>"Subject One","template"=>"email_one"),
array("to"=>"some_other_user@domain.com","bcc"=>"","from"=>"admin@domain.com","subject"=>"Subject Two","template"=>"email_two")
);
 
or if you are editing within Admin->module settings, you can enter nested arrays as follows:
 
{to=admin@site.com,from=some@domain.com,subject=Subject One for Admin Email,template=name_of_email_
template_for_admin,}
{to=user@site.com,from=some@domain.com,subject=Subject Two for User Email ,template=name_of_email_template_for_user,}