Author Topic: Weather Banner Help  (Read 284 times)

0 Members and 1 Guest are viewing this topic.

Offline 44085weather

  • Senior Member
  • **
  • Posts: 60
    • 44085weather.com
Weather Banner Help
« on: April 07, 2017, 05:58:48 PM »
So I added the weather banner script from TNET, and it is working, it there a way to add an image to the background? I see alot of you here have very nice banners that show this data. is there another script I should be using? Also, is there a way for the script to generate a .jpg file?
I would like to use this image -
Here is the code form the page:
Code: [Select]
<?php
############################################################################
# A Project of TNET Services, Inc.
############################################################################
#
#   Project:    Cumulus Project 
#   Package:    Cumulus Weather Banner
#   Module:     banner.php
#   Version:    3.0 - October 19th, 2008
#   Purpose:    Outputs a plain weather graphic based upon
#               the Cumulus realtime.txt file
#   Authors:    Kevin W. Reed <programs@tnet.com>
#               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 4th 2008 
#       Initial Release
#
#   1.1 Released Oct 5th 2008
#       Added TTF Support
#       Added Check for Rain and if none output No Rain Today
#
#   1.2 Released Oct 5th 2008
#       Corrected ISO Date format, was missing the 20 to make the year
#       a 4 digit value.  Realtime.txt data has it as a 2 digit year
#       value.
#
#   1.3 Released Oct 6th 2008
#       Added new field $SITE['dateiso']  If yes, the date is formated
#       in ISO format (YYYY-MM-DD), else it will use a worded month
#       Date (Oct 6 2008)
#
#   1.4 Released Oct 8th 2008
#       Increased the default size of the Cumulus Version Date line to
#       make it more readable. 
#
#   2.0 Released Oct 9th 2008
#       Reconfigured to use new write functions that provide right, left
#       center to position the text more accurately
#
#   3.0 Released Oct 19th, 2008
#       Reconfigured to use extended realtime.txt format that was
#       Released with Cumulus 1.8.2 Beta.
#
############################################################################
#   This document uses Tab 4 Settings
############################################################################
$DATA   = array();
$SITE   = array();
############################################################################
# CONFIGURATION INFORMATION
############################################################################
$SITE['version']        = "3.0";
$SITE['hloc']           = "./";
$SITE['datafile']       = "realtime.txt";
$SITE['image_width']    = 468;
$SITE['image_height']   = 60;
#---------------------------------------------------------------------------
$SITE['fontdir']        = "fonts/";     # must contain ttf font files!
$SITE['usettf']         = "no";        # Set to No to use default fonts
#---------------------------------------------------------------------------
$SITE['format']         = "png";        # Options: jpeg  gif  png 
$SITE['dateiso']        = "no";
$SITE['sitename']       = "44085WEATHER.COM";
#---------------------------------------------------------------------------
// Current field names (matches tag fields) used
$SITE['cvalues'] = array(
    
"date","time","temp","hum","dew","wspeed","wgust","avgbearing","rrate",
    
"rfall","press","wdir","beaufort","windunit","tempunit","pressunit","rainunit",
    
"windrun","presstrend","rmonth","ryear","rfallY","intemp","inhum","wchill",
    
"temptrendval","tempTH","TtempTH","tempTL","TtempTL",
    
"windTM","TwindTM","wgustTM","TwgustTM",
    
"pressTH","TpressTH","pressTL","TpressTL",
    
"cversion","cbuild");
############################################################################

############################################################################
# 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;
}
############################################################################
# Reference: Cumulus Format of realtime.txt file
############################################################################
#   Field       Pos     Example     Description
#   date        0       18/10/08    date (always dd/mm/yy)
#   time        1       16:03:45    time (always hh:mm:ss)
#   temp        2       8.4         outside temperature
#   hum         3       84          relative humidity
#   dew         4       5.8         dewpoint
#   wspeed      5       24.2        wind speed (average)
#   wgust       6       33.0        wind speed (gust)
#   avgbearing  7       261         wind bearing
#   rrate       8       0.0         current rain rate
#   rfall       9       1.0         rain today
#   press       10      999.7       barometer
#   wdir        11      W           wind direction
#   beaufort    12      6           wind speed (beaufort)
#   windunit    13      mph         wind units
#   tempunit    14      C           temperature units
#   pressunit   15      mb          pressure units
#   rainunit    16      mm          rain units
#   windrun     17      146.6       wind run (today)
#   pressrend   18      +0.1        pressure trend value
#   rmonth      19      85.2        monthly rain
#   ryear       20      588.4       yearly rain
#   rfallY      21      11.6        yesterday's rainfall
#   intemp      22      20.3        inside temperature
#   inhum       23      57          inside humidity
#   wchll       24      3.6         wind chill
#   temptrendval 25     -0.7        temperature trend value
#   tempTH      26      10.9        today's high temp
#   TtempTH     27      12:00       time of today's high temp (hh:mm)
#   tempTL      28      7.8         today's low temp
#   TtempTL     29      14:41       time of today's low temp (hh:mm)
#   windTM      30      37.4        today's high wind speed (average)
#   TwindTM     31      14:38       time of today's hi wind (avg) (hh:mm)
#   wgustTM     32      44.0        today's high wind gust
#   TwgustTM    33      14:28       time of today's high wind gust (hh:mm)
#   pressTH     34      999.8       today's high pressure
#   TpressTH    35      16:01       time of today's high pressure (hh:mm)
#   pressTL     36      998.4       today's low pressure
#   TpressTL    37      12:06       time of today's low pressure (hh:mm)
#   cversion    38      1.8.2       Cumulus version
#   cbuild      39      459         Cumulus build no
############################################################################


// Path to our font file
$font1 $SITE['fontdir'] . "arialbd.ttf";
$font2 $SITE['fontdir'] . "arial.ttf";
$font3 $SITE['fontdir'] . "verdana.ttf";

// Read data into array
$DATA get_raw($SITE['hloc'] . $SITE['datafile'],' ');

$months = array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug",
    
"Sep","Oct","Nov","Dec");

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
    
$DATA[0] = $months[intval(substr($DATA[0],3,2)) -] . 
        
" " 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($im0xFF0xFF0xFF);
$black              imagecolorallocate($im0x000x000x00);
$green              imagecolorallocate($im0x000x800x00);
$blue               imagecolorallocate($im0x000x000xff);
$red                imagecolorallocate($im0xff0x000x00);

// Make a Border Around the box
ImageRectangle($im00$SITE['image_width'] -
    
$SITE['image_height'] -1$black);

// Print Station Name
$text $SITE['sitename'];

if (
$SITE['usettf'] == "yes" ) {
    
$size 11;
    
imagettftextbox($im$size004$green$font1$text$SITE['image_width'], "left");
} else {
    
$size 5;
    
imageWriteString($im$size$text$green"left"3);
}

// Print Date (top right corner)
$text ret_value("date") . ' @ ' ret_value("time");
if (
$SITE['usettf'] == "yes" ) {
    
$size 8;
    
imagettftextbox($im$size001$green$font3$text$SITE['image_width'], "right");
} else {
    
$size 2;
    
imageWriteString($im$size$text$green"right"1);
}


// Print Cumulus version info
$text "Cumulus v" ret_value("cversion") . ' #' ret_value("cbuild");
if (
$SITE['usettf'] == "yes" ) {
    
$size 6;
    
imagettftextbox($im$size0014$green$font3$text$SITE['image_width'], "right");
} else {
    
$size 2;
    
imageWriteString($im$size$text$green"right"11);
}

// Print Temp
$text ret_value("temp") . '' ret_value("tempunit");
if (
$SITE['usettf'] == "yes" ) {
    
$size 18;
    
imagettftextbox($im$size0020$red$font1$text$SITE['image_width'], "center");
} else {
    
$size 5;
    
imageWriteString($im$size$text$red"center"23);
}

// Print Humitidy and Dewpt Centered
$text "Hum: " ret_value("hum") . "% - " "DewPt: " ret_value("dew") . '' ret_value("tempunit");
if (
$SITE['usettf'] == "yes" ) {
    
$size 10;
    
imagettftextbox($im$size0041$black$font2$text$SITE['image_width'], "center");
} else {
    
$size 3;
    
imageWriteString($im$size$text$black"center"40);
}

// Print windspeed
$text "Wind: " ret_value("wdir") . " @ " ret_value("wspeed") . ret_value("windunit");
if (
$SITE['usettf'] == "yes" ) {
    
$size 10;
    
imagettftextbox($im$size0022$black$font3$text$SITE['image_width'], "left");
} else {
    
$size 5;
    
imageWriteString($im$size$text$black"left"23);
}


// Print Wind gust
$text "Gust: " ret_value("wgust") . ret_value("windunit");
if (
$SITE['usettf'] == "yes" ) {
    
$size 8;
    
imagettftextbox($im$size0040$black$font2$text$SITE['image_width'], "left");
} else {
    
$size 2;
    
imageWriteString($im$size$text$black"left"39);
}


// Print Rain
if (ret_value("rfall") == "0.00" ) {
    
$text "No Rain Today";
    
$py 22;
} else {
    
$text "   Rain: " ret_value("rfall") . ' ' ret_value("rainunit");
    
$py 25;
}
if (
$SITE['usettf'] == "yes" ) {
    
$size 10;
    
imagettftextbox($im$size00$py$blue$font3$text$SITE['image_width'], "right");
} else {
    
$size 5;
    
imageWriteString($im$size$text$blue"right"23);
}


// Print Baro
$text "Baro: " ret_value("press") . ' ' ret_value("pressunit");
if (
$SITE['usettf'] == "yes" ) {
    
$size 8;
    
imagettftextbox($im$size0042$black$font2$text$SITE['image_width'], "right");
} else {
    
$size 2;
    
imageWriteString($im$size$text$black"right"39);
}


// 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$fd8192 );
        }
        
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] - 6);
            }
            if (
$align == "left") {
                
$left_offset 5;
            }
            
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);
}

// Similar function for use with standard GD fonts.

function imageWriteString(&$img$font$text$color$position_x 'center'$position_y 'center') {
    
// initialize internal variables
    
$x null;
    
$y null;

    if(
$font || $font 5){ $font 0; }
    
$num = array(array(4.66),
                 array(
4.66),
                 array(
5.612),
                 array(
6.512),
                 array(
7.616),
                 array(
8.516));

    
$width ceil(strlen($text) * $num[$font][0]);
    
$height $num[$font][1] + 2;

    
// handle position x
    
if (is_string($position_x)) {
        switch(
$position_x) {
            case 
'left':
                
$position_x 5;
                break;
            case 
'right':
                
$position_x = -8;
                break;
            default:
            case 
'center':
                
$x     = (imagesx($img) - $width 8) / 2;
                break;
        }
    }
    if (!isset(
$x)) {
        if(
is_numeric($position_x)) {
            if (
$position_x >= 0) {
                
$x $position_x// left
            
} else {
                
$x imagesx($img) - $width $position_x 8// right
            
}
        } else {
            
$x     = (imagesx($img) - $width 8) / 2// default / error value: center
        
}
    }

    
// handle position y
    
if (is_string($position_y)) {
        switch(
$position_y) {
            case 
'top':
                
$position_y 5;
                break;
            case 
'bottom':
                
$position_y = -5;
                break;
            default:
            case 
'center':
                
$y     = (Imagesy($img) - ($num[$font][1] + 2))/2;
                break;
        }
    }
    if (!isset(
$y)) {
        if (
is_numeric($position_y)) {
            if (
$position_y >= 0) {
                
$y $position_y// top
            
} else {
                
$y Imagesy($img) - $height $position_y// bottom
            
}
        } else {
            
$y     = (Imagesy($img) - ($num[$font][1] + 2))/2// default / error value: center
        
}
    }
    
imagestring($img$font$x$y$text$color);
}

function 
ret_value($lookup) {
    global 
$SITE$DATA;
    
    
$rtn array_search  $lookup  $SITE['cvalues'] );
    
    if (
$rtn !== FALSE) {
        return( 
$DATA[$rtn] );
    } else {
        return(
"-");
    }
}

############################################################################
# END OF MODULES
############################################################################


Offline 44085weather

  • Senior Member
  • **
  • Posts: 60
    • 44085weather.com
Re: Weather Banner Help
« Reply #2 on: April 07, 2017, 08:26:08 PM »

This may help...   http://www.wxforum.net/index.php?topic=30948.msg307746#msg307746


Enjoy,
Paul
Thanks Paul.

Also, is there a way for the script to generate a .jpg file? I cannot figure out a way to use the image certain places that require. An image file and not a php file
« Last Edit: April 07, 2017, 08:30:30 PM by 44085weather »

Offline PaulMy

  • Forecaster
  • *****
  • Posts: 3447
    • KomokaWeather
Re: Weather Banner Help
« Reply #3 on: April 07, 2017, 09:04:01 PM »
Sorry I can't help on creating an image file.  Way beyond day expertise of cut and paste.


Enjoy,

Paul

 

anything