# TNET Services, Inc. # Copyright: (c) 1992-2008 Copyright TNET Services, Inc. # # License: # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, # USA. # ############################################################################ # HISTORY ############################################################################ # # 1.0 Released Oct 8th, 2008 # Inital Release # ############################################################################ # This document uses Tab 4 Settings ############################################################################ $DATA = array(); $SITE = array(); ############################################################################ # CONFIGURATION INFORMATION ############################################################################ $SITE['version'] = "0.1"; $SITE['hloc'] = "./"; $SITE['datafile'] = "realtime.txt"; $SITE['fontdir'] = "fonts/"; # must contain ttf font files! $SITE['usettf'] = "yes"; # Set to No to use default fonts $SITE['format'] = "png"; # Options: jpeg gif png $SITE['dateiso'] = "no"; $SITE['image_width'] = 100; $SITE['image_height'] = 100; $SITE['sitename'] = "Mesa AZ USA"; $SITE['cumulusver'] = "1.8.2"; $SITE['tempunit'] = "°F"; $SITE['rainunit'] = "in"; $SITE['barounit'] = " mb"; $SITE['windunit'] = " mph"; ############################################################################ ############################################################################ # SOURCECODE VIEW ############################################################################ if ( isset($_REQUEST['view']) && $_REQUEST['view'] == 'sce' ) { $filenameReal = __FILE__; $download_size = filesize($filenameReal); header('Pragma: public'); header('Cache-Control: private'); header('Cache-Control: no-cache, must-revalidate'); header("Content-type: text/plain"); header("Accept-Ranges: bytes"); header("Content-Length: $download_size"); header('Connection: close'); readfile($filenameReal); exit; } ############################################################################ # Cumulus Format of realtime.txt file ############################################################################ # [0] => 04/10/08 Date # [1] => 18:33:01 Time # [2] => 75.1 Temp # [3] => 50 Humidity # [4] => 55.2 DewPt # [5] => 1.0 Wind Speed # [6] => 0.0 Wind Gust # [7] => 5 Bearing (Compass) # [8] => 0.00 Rain Rate # [9] => 0.00 Rain Today # [10] => 1004.1 Baro # [11] => N Windir Label # [12] => 1 Wind Speed # [13] => mph Wind Type ############################################################################ // Path to our font file $font1 = $SITE['fontdir'] . "arialbd.ttf"; # Arial Bold $font2 = $SITE['fontdir'] . "arial.ttf"; # Arial $font3 = $SITE['fontdir'] . "verdana.ttf"; # Verdana // Read data into array $DATA = get_raw($SITE['hloc'] . $SITE['datafile'],' '); // Adjustments to Date and Time obtained from realtime.txt $months = array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug", "Sep","Oct","Nov","Dec"); // If ISO date set then convert the date into an ISO format // YYYY-MM-DD if ( $SITE['dateiso'] == "yes" ) { // Fix date to use ISO formatted date $DATA[0] = "20" . substr($DATA[0],6,2) . '-' . substr($DATA[0],3,2) . '-' . substr($DATA[0],0,2); } else { // Fix date to use Month formatted date // Mon xx YYYY $DATA[0] = $months[intval(substr($DATA[0],3,2)) -1 ] . " " . intval(substr($DATA[0],0,2)) . " " . "20" . substr($DATA[0],6,2); } // Fix time by removing seconds from the field $DATA[1] = substr($DATA[1],0,5); // Output the header to let the browser know we are // sending a graphic file format header("Content-type: image/" . $SITE['format'] ); $im = @imagecreate($SITE['image_width'], $SITE['image_height']) or die("Cannot Initialize new GD image stream"); // Set colors we want to use... // first background is a white background #$background_color = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); $background_color = imagecolorallocate($im, 247,247,247); $black = imagecolorallocate($im, 0x00, 0x00, 0x00); $green = imagecolorallocate($im, 0x00, 0x80, 0x00); $blue = imagecolorallocate($im, 0x00, 0x00, 0xff); $red = imagecolorallocate($im, 0xff, 0x00, 0x00); // Make a Border Around the box ImageRectangle($im, 0, 0, $SITE['image_width'] -1 , $SITE['image_height'] -1, $black); // Print Station Name $text = $SITE['sitename']; if ($SITE['usettf'] == "yes" ) { $size = 11; imagettftextbox($im, $size, 0, 0, 2, $green, $font1, $text, 100, "center"); } else { center_text ( $im, 4, $text, 2, $green); } // Print Cumulus Version Center bottom $text = "Cumulus v" . $SITE['cumulusver']; if ($SITE['usettf'] == "yes" ) { $size = 7; imagettftextbox($im, $size, 0, 0, 79, $green, $font2, $text, 100, "center"); } else { center_text ( $im, 1, $text, 81, $green); } // Print Date Center Bottom $text = $DATA[0] . ' @ ' . $DATA[1]; if ($SITE['usettf'] == "yes" ) { $size = 7; imagettftextbox($im, $size, 0, 0, 86, $black, $font2, $text, 100, "center"); } else { center_text ( $im, 1, $text, 89, $black); } // Print Temp $text = $DATA[2] . $SITE['tempunit']; if ($SITE['usettf'] == "yes" ) { $size = 13; imagettftextbox($im, $size, 0, 0, 16, $red, $font1, $text, 100, "center"); } else { center_text ( $im, 5, $text, 16, $red); } // Print Humitidy and Dewpt Centered $text = "Hum " . $DATA[3] . "% Dew " . $DATA[4] . $SITE['tempunit']; if ($SITE['usettf'] == "yes" ) { $size = 7; imagettftextbox($im, $size, 0, 0, 33, $green, $font2, $text, 100, "center"); } else { center_text ( $im, 1, $text, 33, $green); } // Print Humitidy and Dewpt Centered $text = $DATA[11] . " @ " . $DATA[5] . $SITE['windunit']; if ($SITE['usettf'] == "yes" ) { $size = 8; imagettftextbox($im, $size, 0, 0, 42, $black, $font2, $text, 100, "center"); } else { center_text ( $im, 2, $text, 42, $green); } // Print Rain // Check to see if there was any rain. if not say so if ($DATA[9] == "0.00" ) { $text = "No Rain Today"; $px = 55; } else { $text = "Rain: " . $DATA[9] . $SITE['rainunit']; $px = 57; } if ($SITE['usettf'] == "yes" ) { $size = 8; imagettftextbox($im, $size, 0, 0, $px, $blue, $font1, $text, 100, "center"); } else { center_text ( $im, 2, $text, 57, $blue); } // Print Baro $text = $DATA[10] . $SITE['barounit']; if ($SITE['usettf'] == "yes" ) { $size = 8; imagettftextbox($im, $size, 0, 0, 69, $black, $font3, $text, 100, "center"); } else { center_text ( $im, 2, $text, 69, $black); } // Output the image in the format we are setup. if ($SITE['format'] == "png" ) { imagepng($im); } if ($SITE['format'] == "jpeg" ) { imagejpeg($im); } if ($SITE['format'] == "gif" ) { imagegif($im); } // We be done... destroy the image imagedestroy($im); exit; ############################################################################ # FUNCTIONS ############################################################################ // Function to read the data file with the // Delimiter provided function get_raw( $rawfile , $del ) { $rawdata = array(); $fd = fopen( $rawfile, "r" ); if ( $fd ) { $rawinfo = ''; while (! feof ( $fd ) ) { $rawinfo .= fread( $fd, 8192 ); } fclose($fd); $rawdata = explode ( $del, $rawinfo ); } else { $rawdata[0]= -9999; } return $rawdata; } // function outputs a string centered in the image function center_text ( $image, $size, $string, $y, $color) { $px = ((imagesx($image)/2) - ( ImageFontWidth($size) * strlen($string) ) / 2); imagestring($image, $size, $px, $y,$string, $color); } // Function snarfed off of a PHP forum. User sk89q function imagettftextbox(&$image, $size, $angle, $left, $top, $color, $font, $text, $max_width, $align) { $text_lines = explode("\n", $text); // Supports line breaks! $lines = array(); $line_widths = array(); $largest_line_height = 0; foreach($text_lines as $block) { $current_line = ''; // Reset current line $words = explode(' ', $block); // Split the text array of single words $first_word = TRUE; $last_width = 0; for($i = 0; $i < count($words); $i++) { $item = $words[$i]; $dimensions = imagettfbbox($size, $angle, $font, $current_line . ($first_word ? '' : ' ') . $item); $line_width = $dimensions[2] - $dimensions[0]; $line_height = $dimensions[1] - $dimensions[7]; if($line_height > $largest_line_height) $largest_line_height = $line_height; if($line_width > $max_width && !$first_word) { $lines[] = $current_line; $line_widths[] = $last_width ? $last_width : $line_width; /*if($i == count($words)) { continue; }*/ $current_line = $item; } else { $current_line .= ($first_word ? '' : ' ') . $item; } if($i == count($words) - 1) { $lines[] = $current_line; $line_widths[] = $line_width; } $last_width = $line_width; $first_word = FALSE; } if($current_line) { $current_line = $item; } } $i = 0; foreach($lines as $line) { if($align == "center") { $left_offset = ($max_width - $line_widths[$i]) / 2; } elseif($align == "right") { $left_offset = ($max_width - $line_widths[$i]); } imagettftext($image, $size, $angle, $left + $left_offset, $top + $largest_line_height + ($largest_line_height * $i), $color, $font, $line); $i++; } return $largest_line_height * count($lines); } ############################################################################ # END OF MODULES ############################################################################