<?php

/*

   RatingQuote
   
     *********  COPYRIGHT AND DISCLAIMER  *********
     
   This software written by Leo Wong, Leo@sweetapp.com
   Copyright (c) 2001 Leo Wong
   All RIghts Reserved

   This software has been placed in the public domain by Leo Wong. Permission is given to distribute and
   use this software in whole or in part. If this software is included, in whole or in part, in software or
   other distributions, Leo Wong must be acknowledged as holding copyright to portions of such software.

   No warranty, representation or guarantee, express or implied, is made regarding the
   suitability of this software for any particular purpose, nor that the use of this software will not infringe
   on the intellectual property rights of third parties with respect to any particular use or application and
   specifically disclaims any and all liability arising out of any such use or application, including but not
   limited to, consequential or incidental damages.
   
   Neither the name "RatingQuote" nor the names of its authors or contributors may be used to endorse or promote
   products derived from this software without specific prior written permission.

   This software may be dependent on software written by third parties and use and distribution may
   be further restricted by their conditions for use.

    *********  REVISION HISTORY  *********

   Version 1.0 released 10/28/2001

     *********  DESCRIPTION  **********

   This software connects to an XML-RPC server to request rating data for a player. The software assumes
   a method named "getLast2RatingsByZoneName" will be available and that it will return a multi-dimensional
   array with at least two elements containing a value named "score". The software outputs a header
   "Content-type: image/png" and a graphic image showing the current store and the change from the
   previous score. The graphic is similar to those commonly used to show changes in stock prices.

   The following variables are required:

      zoneName=[string] An MS Gaming Zone name.
       
      gameName=[string] The game you want the rating of for the zoneName.
   
   The following variables can be set but are not required:
   
      graphicHeight=[integer] (Default=104 if button=none. Default=108 if button=plain.)
      graphicWidth=[integer] (Default=18 if button=none. Default=22 if button=plain.)
                             The graphic dimensions can be set but careful consideration must be given to space
                             required for the output. The software does not check whether or not the data will
                             fit into the given space and if it will not the results are unpredictable. Data is
                             always centered vertically and horizontal justification is set by the variable justify.
                             It is possible to customize the font descriptions to create a better looking larger or
                             smaller button. For more information see the font description file.
   
      bgColor=[hex] (Default=FFFFFF) 
                    3 bytes describing the RGB values of the background color. If button!=plain this value is ignored.
   
      button=["none" | "plain" ] (Default="none") 
                                 By default the image is a borderless graphic designed to be easily integrated
                                 into a website. Setting the value to "plain" produces a button instead.
                                 
      justify=["left" | "center" | "right"] (Default="left")
                                            Specify justification of the data in the image.
                                            
      showMinusSign=["y" | "n" ] (Default="y")
                                 Set if the "-" sign will appear if the change in rating is negative.
                                 
      showNoChange=["y" | "n" ] (Default="y")
                                Set if a no change symbol and a "0" will be displayed if there is no change in rating.
                                
      rpcServer=[string] (Default="www.sweetapp.com")
                         Define RPC server.
                         
      rpcServerDir=[string] (Default="/cgi-bin/zone/RPC2")
                            Define the RPC server directory.
                            
      timeout=[integer] (Default=15)
                        Set the length of time in seconds to wait for a response from the RPC server.
                     
   The following files are required:
   
      characterDefs.inc The font description file. See this file's header for more information.
      
      xmlrpc.inc The third-party XML-RPC library. See this file's header for more information.
      
      Additionally, your PHP installation must include the GD library. More information on this library can
      be found at http://www.boutell.com/gd/ or at www.php.net .
      
   Error descriptions:
   
      "Bad Params" This occurs when a required variable was not set.
      
      "No Server" This occurs both when a server could not be located (i.e. the URL could not be resolved), the
                  server did not respond in less than timeout seconds, or the server returned any error.
                  
      "No Data Yet" This occurs when a zoneName was not known by the server or when the zoneName did not have any
                    data recorded yet. If this error was returned by the default server the zoneName is automatically
                    added and recording will start for that zoneName.
   
*/


    
include ('xmlrpc.inc');
    
include ('characterDefs.inc');

    
if (!isset($button)) $button="none";
        
else
        
{
            
$button=strtolower($button);
            
if ($button!="plain") $button="none";
        
}

    
if (!isset($graphicHeight))
    
{
        
$graphicHeight=18;
        
if ($button=="plain") $graphicHeight+=4;
    
}
    
    
if (!isset($graphicWidth))
    
{
        
$graphicWidth=104;
        
if ($button=="plain") $graphicWidth+=4;
    
}
    
    
if ($button=="plain")
    
{
        
$im=imagecreate($graphicWidth, $graphicHeight);
        
$border=2;
    
}
        
else
        
{
            
$im=imagecreate($graphicWidth, $graphicHeight);
            
$border=0;
        
}

    
if ($button=="plain")
    
{
        
$bgColor=ImageColorAllocate($im, 204, 204, 204);
        
$buttonMidGrey=ImageColorAllocate($im, 134, 134, 134);
        
$buttonDarkGrey=ImageColorAllocate($im, 66, 66, 66);
        
$white=ImageColorAllocate($im, 255, 255, 255);
    
}
        
elseif (!isset($bgColor)||hexdec($bgColor)>0xFFFFFF||hexdec($bgColor)<0||strlen($bgColor)>6) $bgColor=ImageColorAllocate($im, 255, 255, 255);
            
else
            
{
                
$i=0;
                
while (substr($bgColor, $i, 1)=="0") $i++;
                
$trimmedbgColor=substr($bgColor, $i, strlen($bgColor)-$i);
                
if (dechex(hexdec($bgColor))!=strtolower($trimmedbgColor)) $bgColor=ImageColorAllocate($im, 255, 255, 255);
                    
else $bgColor=ImageColorAllocate($im, hexdec(substr($bgColor, 0, 2)), hexdec(substr($bgColor, 2, 2)), hexdec(substr($bgColor, 4, 2)));
            
}

    imagefill
($im, 0, 0, $bgColor);

    
if ($button=="plain")
    
{
        imageline
($im, 1, 1, $graphicWidth-3, 1, $white);
        imageline
($im, 1, 2, 1, $graphicHeight-3, $white);
        imageline
($im, 1, $graphicHeight-2, $graphicWidth-2, $graphicHeight-2, $buttonMidGrey);
        imageline
($im, $graphicWidth-2, 1, $graphicWidth-2, $graphicHeight-3, $buttonMidGrey);
        imageline
($im, 0, $graphicHeight-1, $graphicWidth-1, $graphicHeight-1, $buttonDarkGrey);
        imageline
($im, $graphicWidth-1, 0, $graphicWidth-1, $graphicHeight-2, $buttonDarkGrey);
    
}

    
$baseColor[0]=$bgColor;
    
$baseColor[1]=ImageColorAllocate($im, 208, 208, 208);
    
$baseColor[2]=ImageColorAllocate($im, 104, 104, 104);
    
$baseColor[3]=ImageColorAllocate($im, 0, 0, 0);

    
if (!isset($zoneName)||!isset($gameName))
    
{
        
$errorColor[0]=$bgColor;
        
$errorColor[1]=ImageColorAllocate($im, 255, 255, 0);
        
$errorColor[2]=ImageColorAllocate($im, 104, 104, 104);
        
$errorColor[3]=ImageColorAllocate($im, 0, 0, 0);
        
        
$errorString="Bad Params";
        
$pixels=$bitMap['errorSign'][1]+4;
        
for ($i=0; $i<strlen($errorString); $i++) $pixels+=$bitMap[ord(substr($errorString, $i, 1))][1];
        
if ($justify=="center") $charPos=floor((($graphicWidth-$pixels)/2))+$border;
            
elseif ($justify=="right") $charPos=$graphicWidth-$pixels-$border;
                
else $charPos=1+$border;
        
$bit=2;
        
for ($yPos=ceil((($graphicHeight-$bitMap['errorSign'][0])/2)); $yPos<ceil((($graphicHeight-$bitMap['errorSign'][0])/2))+$bitMap['errorSign'][0];  $yPos++)
        
{
            
for ($xPos=0; $xPos<$bitMap['errorSign'][1]; $xPos++)
            
{
                imagesetpixel
($im, $xPos+$charPos, $yPos, $errorColor[$bitMap['errorSign'][$bit++]]);
            
}
        
}
        
$charPos+=$bitMap['errorSign'][1]+4;
        
for ($i=0; $i<strlen($errorString); $i++)
        
{
            
$character=ord(substr($errorString, $i, 1));
            
$bit=2;
            
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
            
{
                
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                
{
                    imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                
}
            
}
            
$charPos+=$bitMap[$character][1];
        
}
    
}
        
else
        
{
            
if (!isset($rpcServer)) $rpcServer="www.sweetapp.com";
            
if (!isset($rpcServerDir)) $rpcServerDir="/cgi-bin/zone/RPC2";
            
if (!isset($timeout)) $timeout=15;

            
$server=new xmlrpc_client($rpcServerDir, $rpcServer, 80);
            
$message=new xmlrpcmsg('getLast2RatingsByZoneName', array(new xmlrpcval($zoneName, 'string'), new xmlrpcval($gameName, 'string')));
            
                    
            
$result=$server->send($message, $timeout);
            
            
if (!$result||$result->faultCode())
            
{
                
$errorColor[0]=$bgColor;
                
$errorColor[1]=ImageColorAllocate($im, 255, 255, 0);
                
$errorColor[2]=ImageColorAllocate($im, 104, 104, 104);
                
$errorColor[3]=ImageColorAllocate($im, 0, 0, 0);
                
                
$errorString="No Server";
                
$pixels=$bitMap['errorSign'][1]+4;
                
for ($i=0; $i<strlen($errorString); $i++) $pixels+=$bitMap[ord(substr($errorString, $i, 1))][1];
                
if ($justify=="center") $charPos=floor((($graphicWidth-$pixels)/2))+$border;
                    
elseif ($justify=="right") $charPos=$graphicWidth-$pixels-$border;
                        
else $charPos=1+$border;
                
$bit=2;
                
for ($yPos=ceil((($graphicHeight-$bitMap['errorSign'][0])/2)); $yPos<ceil((($graphicHeight-$bitMap['errorSign'][0])/2))+$bitMap['errorSign'][0];  $yPos++)
                
{
                    
for ($xPos=0; $xPos<$bitMap['errorSign'][1]; $xPos++)
                    
{
                        imagesetpixel
($im, $xPos+$charPos, $yPos, $errorColor[$bitMap['errorSign'][$bit++]]);
                    
}
                
}
                
$charPos+=$bitMap['errorSign'][1]+4;
                
for ($i=0; $i<strlen($errorString); $i++)
                
{
                    
$character=ord(substr($errorString, $i, 1));
                    
$bit=2;
                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                    
{
                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                        
{
                            imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                        
}
                    
}
                    
$charPos+=$bitMap[$character][1];
                
}
            
}
                
else
                
{
                    
$struct=$result->value();
                    
$arr=xmlrpc_decode($struct);
                    
if (!$arr[0]['score'])
                    
{
                        
$errorColor[0]=$bgColor;
                        
$errorColor[1]=ImageColorAllocate($im, 255, 255, 0);
                        
$errorColor[2]=ImageColorAllocate($im, 104, 104, 104);
                        
$errorColor[3]=ImageColorAllocate($im, 0, 0, 0);
                        
                        
$errorString="No Data Yet";
                        
$pixels=$bitMap['errorSign'][1]+4;
                        
for ($i=0; $i<strlen($errorString); $i++) $pixels+=$bitMap[ord(substr($errorString, $i, 1))][1];
                        
if ($justify=="center") $charPos=floor((($graphicWidth-$pixels)/2))+$border;
                            
elseif ($justify=="right") $charPos=$graphicWidth-$pixels-$border;
                                
else $charPos=1+$border;
                        
$bit=2;
                        
for ($yPos=ceil((($graphicHeight-$bitMap['errorSign'][0])/2)); $yPos<ceil((($graphicHeight-$bitMap['errorSign'][0])/2))+$bitMap['errorSign'][0];  $yPos++)
                        
{
                            
for ($xPos=0; $xPos<$bitMap['errorSign'][1]; $xPos++)
                            
{
                                imagesetpixel
($im, $xPos+$charPos, $yPos, $errorColor[$bitMap['errorSign'][$bit++]]);
                            
}
                        
}
                        
$charPos+=$bitMap['errorSign'][1]+4;
                        
for ($i=0; $i<strlen($errorString); $i++)
                        
{
                            
$character=ord(substr($errorString, $i, 1));
                            
$bit=2;
                            
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                            
{
                                
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                
{
                                    imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                
}
                            
}
                            
$charPos+=$bitMap[$character][1];
                        
}
                    
}
                        
else
                        
{
                            
$rating=$arr[0]['score'];
                            
if (!$arr[1]['score']) $arr[1]['score']=$arr[0]['score'];
                            
$change=$arr[0]['score']-$arr[1]['score'];
                            
$ratingString=sprintf("%d", $rating);
                            
if (!isset($showMinusSign)) $showMinusSign="y";
                                
else $showMinusSign=strtolower($showMinusSign);
                            
if ($showMinusSign=="y") $changeString=sprintf("%d", $change);
                                
else $changeString=sprintf("%d", abs($change));
                            
$pixels=0;
                            
for ($i=0; $i<strlen($ratingString); $i++) $pixels+=$bitMap[ord(substr($ratingString, $i, 1))][1];
                            
if ($change==0)
                            
{
                                
if (!isset($showNoChange)) $showNoChange="y";
                                    
else $showNoChange=strtolower($showNoChange);
                                
if ($showNoChange=="y") $pixels=$pixels+$bitMap[32][1]+$bitMap['ncArrow'][1]+$bitMap[48][1];
                                
if ($justify=="center") $charPos=floor((($graphicWidth-$pixels)/2))+$border;
                                    
elseif ($justify=="right") $charPos=$graphicWidth-$pixels-$border;
                                        
else $charPos=$border;
                                
for ($i=0; $i<strlen($ratingString); $i++)
                                
{
                                    
$character=ord(substr($ratingString, $i, 1));
                                    
$bit=2;
                                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                    
{
                                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                        
{
                                            imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                        
}
                                    
}
                                    
$charPos+=$bitMap[$character][1];
                                
}
                                
if ($showNoChange=="y")
                                
{
                                    
$character=32;
                                    
$bit=2;
                                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                    
{
                                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                        
{
                                            imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                        
}
                                    
}
                                    
$charPos+=$bitMap[$character][1];
                                    
$character="ncArrow";
                                    
$bit=2;
                                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                    
{
                                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                        
{
                                            imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                        
}
                                    
}
                                    
$charPos+=$bitMap[$character][1];
                                    
$character=48;
                                    
$bit=2;
                                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                    
{
                                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                        
{
                                            imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                        
}
                                    
}
                                
}
                            
}
                                
else
                                
{
                                    
$pixels+=$bitMap[32][1];
                                    
if ($change<0)
                                    
{
                                        
$changeColor[0]=$bgColor;
                                        
$changeColor[1]=ImageColorAllocate($im, 255, 208, 208);
                                        
$changeColor[2]=ImageColorAllocate($im, 255, 104, 104);
                                        
$changeColor[3]=ImageColorAllocate($im, 255, 0, 0);
                                        
$pixels+=$bitMap['downArrow'][1];
                                    
}
                                        
elseif ($change>0)
                                        
{
                                            
$changeColor[0]=$bgColor;
                                            
$changeColor[1]=ImageColorAllocate($im, 154, 205, 154);
                                            
$changeColor[2]=ImageColorAllocate($im, 104, 180, 104);
                                            
$changeColor[3]=ImageColorAllocate($im, 0, 128, 0);
                                            
$pixels+=$bitMap['downArrow'][1];
                                        
}
                                    
for ($i=0; $i<strlen($changeString); $i++) $pixels+=$bitMap[ord(substr($changeString, $i, 1))][1];
                                    
if ($justify=="center") $charPos=floor((($graphicWidth-$pixels)/2))+$border;
                                        
elseif ($justify=="right") $charPos=$graphicWidth-$pixels-$border;
                                            
else $charPos=$border;
                                    
for ($i=0; $i<strlen($ratingString); $i++)
                                    
{
                                        
$character=ord(substr($ratingString, $i, 1));
                                        
$bit=2;
                                        
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                        
{
                                            
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                            
{
                                                imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                            
}
                                        
}
                                        
$charPos+=$bitMap[$character][1];
                                    
}
                                    
$character=32;
                                    
$bit=2;
                                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                    
{
                                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                        
{
                                            imagesetpixel
($im, $xPos+$charPos, $yPos, $baseColor[$bitMap[$character][$bit++]]);
                                        
}
                                    
}
                                    
$charPos+=$bitMap[$character][1];
                                    
if ($change>0) $character="upArrow";
                                        
else $character="downArrow";
                                    
$bit=2;
                                    
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                    
{
                                        
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                        
{
                                            imagesetpixel
($im, $xPos+$charPos, $yPos, $changeColor[$bitMap[$character][$bit++]]);
                                        
}
                                    
}
                                    
$charPos+=$bitMap[$character][1];
                                    
for ($i=0; $i<strlen($changeString); $i++)
                                    
{
                                        
$character=ord(substr($changeString, $i, 1));
                                        
$bit=2;
                                        
for ($yPos=ceil((($graphicHeight-$bitMap[$character][0])/2)); $yPos<ceil((($graphicHeight-$bitMap[$character][0])/2))+$bitMap[$character][0];  $yPos++)
                                        
{
                                            
for ($xPos=0; $xPos<$bitMap[$character][1]; $xPos++)
                                            
{
                                                imagesetpixel
($im, $xPos+$charPos, $yPos, $changeColor[$bitMap[$character][$bit++]]);
                                            
}
                                        
}
                                        
$charPos+=$bitMap[$character][1];
                                    
}
                                
}
                        
}
                
}
        
}

    header 
("Content-type: image/png");   
    ImagePng
($im);
    imagedestroy
($im);

?>    

Download
The source was styled using SilverCity