I have built a module that creates an image based on data that a user inputs into a cck field. Basically the module queries the drupal database, gets the data, then it uses imagecreate and imagepng to build an image based on that data.
The .module code I have so far is below. As you can see, I call "dangerrose.png" to generate the image and use session variables to pass the nid so that I tie the node to the image. I would like to make a more permanent attachment to the node so that I could use views to display several nodes and their associated images as a table. I really want to save the image created as a file attached to the node whose data created the image.
I am relatively new to module development in drupal so any ideas/help would be greatly appreciated.
function dangerrose_menu() {
$items['dangerrose.png'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'dangerrose_image',
'access arguments' => array('access content'), );
return $items;
}
function dangerrose_image() {
drupal_set_header("Content-type: image/png");
$nodeid = $_SESSION['nodeid'];
$query = "SELECT node.nid AS nid, node_data_field_abeast.field_abeast_value AS abeast, node.type AS node_type, node.vid AS node_vid, node_data_field_abeast.field_abnorth_value AS abnorth, node_data_field_abeast.field_abnortheast_value AS abnortheast, node_data_field_abeast.field_abnorthwest_value AS abnorthwest, node_data_field_abeast.field_absouth_value AS absouth, node_data_field_abeast.field_absoutheast_value AS absoutheast, node_data_field_abeast.field_absouthwest_value AS absouthwest, node_data_field_abeast.field_abwest_value AS abwest, node_data_field_abeast.field_bnorthwest_value AS bnorthwest, node_data_field_abeast.field_bnorth_value AS bnorth, node_data_field_abeast.field_bnortheast_value AS bnortheast, node_data_field_abeast.field_beast_value AS beast, node_data_field_abeast.field_bsoutheast_value AS bsoutheast, node_data_field_abeast.field_bsouth_value AS bsouth, node_data_field_abeast.field_bsouthwest_value AS bsouthwest, node_data_field_abeast.field_bwest_value AS bwest, DATE_FORMAT((FROM_UNIXTIME(node.created) + INTERVAL -28800 SECOND), '%Y%m%d%H%i') AS node_created_minute FROM node node LEFT JOIN content_type_forecast node_data_field_abeast ON node.vid = node_data_field_abeast.vid WHERE (node.status = 1) AND (node.type in ('forecast')) AND (node.nid = $nodeid)";
$avyforecast = db_query($query);
$avyinfo = db_fetch_array($avyforecast);
// create a blank image
$image = imagecreate(275, 171);
// fill the background color
$bg = imagecolorallocate($image, 244, 239, 220);
// allocate some solors
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$green = imagecolorallocate($image, 0, 255, 0);
$yellow = imagecolorallocate($image, 255, 255, 0);
$orange = imagecolorallocate($image, 255, 163, 25);
$red = imagecolorallocate($image, 255, 25, 0);
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);
$gray = imagecolorallocate($image, 178, 178, 178);
$darkgreen = imagecolorallocate($image, 72, 173, 5);
$darkyellow = imagecolorallocate($image, 235, 230, 20);
$darkorange = imagecolorallocate($image, 227, 139, 17);
$darkred = imagecolorallocate($image, 178, 34, 34);
// insert letters for the various directions
imagestring($image, 5, 76, -2, "N", $black);
imagestring($image, 5, 4, 72, "W", $black);
imagestring($image, 5, 150, 72, "E", $black);
imagestring($image, 5, 76, 142, "S", $black);
imagestring($image, 3, 127, 21, "NE", $gray);
imagestring($image, 3, 20, 21, "NW", $gray);
imagestring($image, 3, 127, 120, "SE", $gray);
imagestring($image, 3, 20, 120, "SW", $gray);
//insert text for the to point ot help link
imagestring($image, 2, 160, 35, "(What's this?)", $gray);
// puts the "danger ratings" info into the $avyinfo array
$hn = $avyinfo[abnorth];
$hne = $avyinfo[abnortheast];
$he = $avyinfo[abeast];
$hse = $avyinfo[absoutheast];
$hs = $avyinfo[absouth];
$hsw = $avyinfo[absouthwest];
$hw = $avyinfo[abwest];
$hnw = $avyinfo[abnorthwest];
$ln = $avyinfo[bnorth];
$lne = $avyinfo[bnortheast];
$le = $avyinfo[beast];
$lse = $avyinfo[bsoutheast];
$ls = $avyinfo[bsouth];
$lsw = $avyinfo[bsouthwest];
$lw = $avyinfo[bwest];
$lnw = $avyinfo[bnorthwest];
$A=1.0;
$B=1.5;
$C=2.0;
$D=2.5;
$E=3.0;
$F=3.5;
$G=4.0;
$H=4.5;
$I=5.0;
// array positions: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ColorArray = array($ln,$lne,$le,$lse,$ls,$lsw,$lw,$lnw,$hn,$hne,$he,$hse,$hs,$hsw,$hw,$hnw);
$PktArray = array();
$PieArray = array();
//$ShdwArray = array();
// Make arrays for pies and pockets
$CB = 0;
while ($CB < 16)
{
$J = $ColorArray[$CB];
if ($J==$A) {$Pie="$green";$Pkt="$green";}
elseif ($J==$B) {$Pie="$green";$Pkt="$yellow";}
elseif ($J==$C) {$Pie="$yellow";$Pkt="$yellow";}
elseif ($J==$D) {$Pie="$yellow";$Pkt="$orange";}
elseif ($J==$E) {$Pie="$orange";$Pkt="$orange";}
elseif ($J==$F) {$Pie="$orange";$Pkt="$red";}
elseif ($J==$G) {$Pie="$red";$Pkt="$red";}
elseif ($J==$H) {$Pie="$red";$Pkt="$black";}
elseif ($J==$I) {$Pie="$black";$Pkt="$black";}
array_push($PieArray,$Pie);
array_push($PktArray,$Pkt);
$CB=$CB+1;
}
// make the pie for the Low tier
imagefilledarc($image, 80, 77, 130, 130, 247, 292, $PieArray[0], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 292, 337, $PieArray[1], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 337, 22, $PieArray[2], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 22, 67, $PieArray[3], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 67, 112, $PieArray[4], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 112, 157, $PieArray[5], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 157, 202, $PieArray[6], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 130, 130, 202, 247, $PieArray[7], IMG_ARC_PIE);
// insert pockets on Low tier
imagefilledellipse($image, 80, 22, 12, 12, $PktArray[0]);
imagefilledellipse($image, 120, 39, 12, 12, $PktArray[1]);
imagefilledellipse($image, 135, 77, 12, 12, $PktArray[2]);
imagefilledellipse($image, 120, 115, 12, 12, $PktArray[3]);
imagefilledellipse($image, 80, 132, 12, 12, $PktArray[4]);
imagefilledellipse($image, 40, 115, 12, 12, $PktArray[5]);
imagefilledellipse($image, 25, 77, 12, 12, $PktArray[6]);
imagefilledellipse($image, 40, 39, 12, 12, $PktArray[7]);
// make the pie for the High tier
imagefilledarc($image, 80, 77, 90, 90, 247, 292, $PieArray[8], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 292, 337, $PieArray[9], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 337, 22, $PieArray[10], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 22, 67, $PieArray[11], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 67, 112, $PieArray[12], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 112, 157, $PieArray[13], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 157, 202, $PieArray[14], IMG_ARC_PIE);
imagefilledarc($image, 80, 77, 90, 90, 202, 247, $PieArray[15], IMG_ARC_PIE);
// insert pockets on High tier
imagefilledellipse($image, 80, 50, 12, 12, $PktArray[8]);
imagefilledellipse($image, 99, 58, 12, 12, $PktArray[9]);
imagefilledellipse($image, 107, 77, 12, 12, $PktArray[10]);
imagefilledellipse($image, 99, 96, 12, 12, $PktArray[11]);
imagefilledellipse($image, 80, 104, 12, 12, $PktArray[12]);
imagefilledellipse($image, 61, 96, 12, 12, $PktArray[13]);
imagefilledellipse($image, 53, 77, 12, 12, $PktArray[14]);
imagefilledellipse($image, 61, 58, 12, 12, $PktArray[15]);
// draw the ellipse around edge of each tier
imageellipse($image, 80, 77, 130, 130, $black);
imageellipse($image, 80, 77, 90, 90, $black);
// draw lines that divide up the rose
imageline($image, 55, 17, 105, 137, $black);
imageline($image, 56, 137, 104, 17, $black);
imageline($image, 20, 102, 140, 52, $black);
imageline($image, 20, 53, 140, 101, $black);
// draw lines to indicate elevations
imageline($image, 85, 122, 110, 147, $black);
imageline($image, 90, 142, 110, 162, $black);
// insert letters to indicate elevation
imagestring($image, 3, 112, 142, "Near and above treeline", $black);
imagestring($image, 3, 112, 158, "Below treeline", $black);
/*
// draw rectangles for the danger rating key
imagefilledrectangle($image, 0, 110, 30, 160, $green);
imagefilledrectangle($image, 0, 60, 30, 110, $yellow);
imagefilledrectangle($image, 0, 35, 30, 60, $orange);
imagefilledrectangle($image, 0, 15, 30, 35, $red);
imagefilledrectangle($image, 0, 0, 30, 15, $black);
imagerectangle($image, 0, 0, 30, 159, $black);
//insert danger ratings into key
imagestring($image, 3, 5, 130, "LOW", $black);
imagestring($image, 3, 5, 80, "MOD", $black);
imagestring($image, 3, 2, 41, "CONS", $black);
imagestring($image, 3, 2, 19, "HIGH", $black);
imagestring($image, 3, 5, 2, "EXT", $red);
*/
// flush image
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
//Stop executing of the scripts to disable sending Drupal’s outputs at the end of the image.
exit();
}
Comments
d7 field module
Andy,
I've been playing with Field API in D7 as a replacement for the D5/D6 avy module. The idea would be to have a danger rose field (CCK). You can configure it for different elevations, labels, etc.
Want to work together on it?
sounds good
That would be great. One of the things I was going to do was create a custom field in for CCK in D6 for now. Unfortunately/fortunately, now that it has started snowing, I will have limited time. Lets touch base and see where we both stand?
when you're ready
Andy,
When you're ready, take a look at the Danger Rose Field module. Trying to keep it flexible enough for centers to use and configure for their needs.
Will do
Thanks for getting a start on this. I will take a look at it as soon as forecasting season winds down (mid to late April).