Advertisement
momo5502

[OLD] Server banner code

Oct 18th, 2012
1,455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.22 KB | None | 0 0
  1. <?php
  2. // ==========================================================
  3. //
  4. // Name: COD/GTA-SAMP Server Banners
  5. //
  6. // Description: Standalone server banner code for MW1/2/3, WAW and GTA-SAMP.
  7. //              Default requirements can be obtained
  8. //              from: http://momo.blackpulse.us/banner/
  9. //              You might need to do some changes to the font code (line 252)
  10. // Initial author: momo5502 <MauriceHeumann@googlemail.com>
  11. // Version: 5.3
  12. // License: Copyright © 2013 momo5502 (Only for private/non-commercial use)
  13. // Credit:
  14. //  -PHP.net as a helpful resource
  15. //  -PixelDemon/alexdahlem for his amazing gdlib tut
  16. //  -BlooDONE for his background watermark
  17. //  -aG`Avail for the online generator (http://momo5504.square7.de/banner.html)
  18. //  -icedream for the api
  19. //  -Richard Perry for some LGSL images
  20. //
  21. // ==========================================================
  22.  
  23.  
  24. //Display document as png
  25. if ( $_GET[ 'debug' ] != "1" )
  26.     header( "Content-Type: image/png" );
  27.  
  28. //Get basic information
  29. if ( isset( $_GET[ 'address' ] ) && $_GET[ 'address' ] != "" ) {
  30.     $address = $_GET[ "address" ];
  31.     $ip      = substr( $address, 0, strpos( $address, ":" ) );
  32.     $port    = substr( $address, strpos( $address, ":" ) + 1 );
  33.    
  34. }
  35.  
  36. else if ( isset( $_GET[ 'adress' ] ) && $_GET[ 'adress' ] != "" ) //OMG a typo - Thx for remarking, Storm
  37.     {
  38.     $address = $_GET[ "adress" ];
  39.     $ip      = substr( $address, 0, strpos( $address, ":" ) );
  40.     $port    = substr( $address, strpos( $address, ":" ) + 1 );
  41.    
  42. }
  43.  
  44. else {
  45.     $ip   = $_GET[ 'ip' ];
  46.     $port = $_GET[ 'port' ];
  47. }
  48.  
  49. //Command that gets written.
  50. //$cmd = "\xFF\xFF\xFF\xFFgetstatus\x00";
  51. $cmd = "\xFF\xFF\xFF\xFFgetinfo\x00";
  52.  
  53. if ( $_GET[ "game" ] == "SAMP" ) {
  54.     $aIPAddr = explode( '.', $ip );
  55.     $cmd     = "SAMP";
  56.     $cmd .= chr( $aIPAddr[ 0 ] );
  57.     $cmd .= chr( $aIPAddr[ 1 ] );
  58.     $cmd .= chr( $aIPAddr[ 2 ] );
  59.     $cmd .= chr( $aIPAddr[ 3 ] );
  60.     $cmd .= chr( $port & 0xFF );
  61.     $cmd .= chr( $port >> 8 & 0xFF );
  62.     $cmd .= 'i';
  63. }
  64.  
  65. //Define the root folder for the external requirements (map previews and image background)
  66. $root = "banner/";
  67.  
  68. if ( $_GET[ "game" ] != "SAMP" ) {
  69.     $connection = getQueryData( $ip, $port, $cmd, true );
  70.     $info       = parseQueryData( $connection, $ip, $port, $cmd );
  71. }
  72.  
  73. else {
  74.     $info = querySAMP( $ip, $port, $cmd );
  75. }
  76. printimage( $info, $root );
  77.  
  78. //Open socket connection, send and receive information, return it & close socket again!
  79. function getQueryData( $ip, $port, $send, $MW3 )
  80. {
  81.     $server  = "udp://" . $ip;
  82.     $connect = @fsockopen( $server, $port, $errno, $errstr, 2 );
  83.    
  84.     if ( !$connect )
  85.         return "-1";
  86.    
  87.    
  88.     else {
  89.         fwrite( $connect, $send );
  90.         stream_set_timeout( $connect, 2 );
  91.         $output = fread( $connect, 8192 );
  92.         $info   = stream_get_meta_data( $connect );
  93.         fclose( $connect );
  94.         if ( $_GET[ 'debug' ] == "1" )
  95.             echo "<big><u>Server response:</u></big><br><br>" . substr( $output, 4 ) . "<br><br><big><u>PNG output:</u></big><br><br>";
  96.        
  97.         if ( !$output || !isset( $output ) || $output == "" || $info[ 'timed_out' ] ) {
  98.             if ( $MW3 )
  99.                 return getMW3Port( $ip, $port, $send );
  100.             else
  101.                 return "-1";
  102.         }
  103.        
  104.         else
  105.             return substr( $output, 4 );
  106.     }
  107. }
  108.  
  109. //MW3 servers use a different port system than other cods
  110. function getMW3Port( $ip, $port, $cmd )
  111. {
  112.     //It uses icedreams api, therefore it only works with 4D1's MW3 servers
  113.     if ( $fp = @fopen( 'http://s.mufff.in/api/cache', 'r' ) ) {
  114.         $content = '';
  115.        
  116.         while ( $line = fgets( $fp, 1024 ) ) {
  117.             $content .= $line;
  118.         }
  119.        
  120.         $data = json_decode( $content );
  121.         $n    = count( $data->iw5m->ListedServers );
  122.         $i    = 0;
  123.         while ( $i < $n ) {
  124.             if ( $ip == $data->iw5m->ListedServers[ $i ]->IPString && $port == $data->iw5m->ListedServers[ $i ]->Ports[ 1 ] ) {
  125.                 return getQueryData( $ip, $data->iw5m->ListedServers[ $i ]->Ports[ 0 ], $cmd, false );
  126.             }
  127.            
  128.             $i++;
  129.         }
  130.        
  131.         return "-1";
  132.        
  133.     } else {
  134.         return "-1";
  135.     }
  136. }
  137.  
  138. //Insert server information into my database.
  139. function insertToDatabase( $info, $width )
  140. {
  141.     if ( isset( $_GET[ 'address' ] ) && $_GET[ 'address' ] != "" ) {
  142.         $address = $_GET[ "address" ];
  143.         $ip      = substr( $address, 0, strpos( $address, ":" ) );
  144.         $port    = substr( $address, strpos( $address, ":" ) + 1 );
  145.        
  146.     }
  147.    
  148.     else if ( isset( $_GET[ 'adress' ] ) && $_GET[ 'adress' ] != "" ) //OMG a typo - Thx for remarking, Storm
  149.         {
  150.         $address = $_GET[ "adress" ];
  151.         $ip      = substr( $address, 0, strpos( $address, ":" ) );
  152.         $port    = substr( $address, strpos( $address, ":" ) + 1 );
  153.        
  154.     }
  155.    
  156.     else {
  157.         $ip   = $_GET[ 'ip' ];
  158.         $port = $_GET[ 'port' ];
  159.     }
  160.     if ( $info[ 'value' ] != "-1" ) {
  161.         if ( $fp = fopen( 'http://momo5504.square7.de/banner_stuff/sql.php?ip=' . $ip . '&port=' . $port . '&width=' . $width . '&color=' . $_GET[ "color" ] . '&game=' . $_GET[ "game" ], 'r' ) )
  162.             fclose( $fp );
  163.     }
  164. }
  165.  
  166. //Get the width of a banner stored in my database, if a server is not reachable.
  167. function getOfflineWidth( )
  168. {
  169.     if ( isset( $_GET[ 'address' ] ) && $_GET[ 'address' ] != "" ) {
  170.         $address = $_GET[ "address" ];
  171.         $ip      = substr( $address, 0, strpos( $address, ":" ) );
  172.         $port    = substr( $address, strpos( $address, ":" ) + 1 );
  173.        
  174.     }
  175.    
  176.     else if ( isset( $_GET[ 'adress' ] ) && $_GET[ 'adress' ] != "" ) //OMG a typo - Thx for remarking, Storm
  177.         {
  178.         $address = $_GET[ "adress" ];
  179.         $ip      = substr( $address, 0, strpos( $address, ":" ) );
  180.         $port    = substr( $address, strpos( $address, ":" ) + 1 );
  181.        
  182.     }
  183.    
  184.     else {
  185.         $ip   = $_GET[ 'ip' ];
  186.         $port = $_GET[ 'port' ];
  187.     }
  188.     $fp      = fopen( 'http://momo5504.square7.de/banner_stuff/getWidth.php?ip=' . $ip . '&port=' . $port, 'r' );
  189.     $content = '';
  190.    
  191.     while ( $line = fgets( $fp, 1024 ) ) {
  192.         $content .= $line;
  193.     }
  194.     fclose( $fp );
  195.     return floatval( substr( $content, 0, strpos( $content, "\n" ) ) );
  196. }
  197.  
  198. //Parse the query data and return it as array.
  199. function parseQueryData( $input, $ip, $port, $cmd )
  200. {
  201.     $server = $ip . ":" . $port;
  202.     $err    = "-";
  203.    
  204.     if ( $input == "-1" )
  205.         $data = array(
  206.              "value" => $input,
  207.             "gametype" => $err,
  208.             "protocol" => $err,
  209.             "clients" => $err,
  210.             "maxclients" => $err,
  211.             "mapname" => $err,
  212.             "server" => $server
  213.         );
  214.    
  215.     else {
  216.         //New segmentation code
  217.        
  218.         if ( $cmd == "\xFF\xFF\xFF\xFFgetstatus\x00" ) {
  219.             $hostname = substr( $input, strpos( $input, "\\sv_hostname" ) + 13 );
  220.             $players  = count( getPlayers( $input ) );
  221.             $gametype = substr( $input, strpos( $input, "\\g_gametype" ) + 12 );
  222.         }
  223.        
  224.         else {
  225.             $hostname = substr( $input, strpos( $input, "\\hostname" ) + 10 );
  226.            
  227.             if ( strpos( $input, "\\clients" ) ) {
  228.                 $players = substr( $input, strpos( $input, "\\clients" ) + 9 );
  229.                 $players = cleanFromRest( $players );
  230.             }
  231.            
  232.             else
  233.                 $players = "-";
  234.            
  235.             $gametype = substr( $input, strpos( $input, "\\gametype" ) + 10 );
  236.         }
  237.        
  238.         $maxplayers = substr( $input, strpos( $input, "\\sv_maxclients" ) + 15 );
  239.         $maxplayers = cleanFromRest( $maxplayers );
  240.        
  241.         $mapname = substr( $input, strpos( $input, "\\mapname" ) + 9 );
  242.         $mapname = cleanFromRest( $mapname );
  243.        
  244.         $hostname = cleanFromRest( $hostname );
  245.        
  246.         $gametype = cleanFromRest( $gametype );
  247.        
  248.         $protocol = substr( $input, strpos( $input, "\\protocol" ) + 10 );
  249.         $protocol = cleanFromRest( $protocol );
  250.        
  251.         //Get a clean hostname without '^1's or '^5's
  252.         $unclean = $hostname;
  253.        
  254.         for ( $i = 0; $i < 10; $i++ )
  255.             $hostname = str_replace( "^{$i}", "", $hostname );
  256.        
  257.         $value = 1;
  258.        
  259.         //Put information into an array
  260.         $data = array(
  261.              "value" => $value,
  262.             "hostname" => $hostname,
  263.             "gametype" => $gametype,
  264.             "protocol" => $protocol,
  265.             "clients" => $players,
  266.             "maxclients" => $maxplayers,
  267.             "mapname" => $mapname,
  268.             "server" => $server,
  269.             "unclean" => $unclean
  270.         );
  271.     }
  272.    
  273.     return $data;
  274. }
  275.  
  276. function getErr( $ip, $port )
  277. {
  278.     $server = $ip . ":" . $port;
  279.     $err    = "-";
  280.    
  281.     $data = array(
  282.          "value" => "-1",
  283.         "gametype" => $err,
  284.         "protocol" => $err,
  285.         "clients" => $err,
  286.         "maxclients" => $err,
  287.         "mapname" => $err,
  288.         "server" => $server
  289.     );
  290.    
  291.     return $data;
  292. }
  293.  
  294. function querySAMP( $ip, $port, $send )
  295. {
  296.     $server  = "udp://" . $ip;
  297.     $connect = @fsockopen( $server, $port, $errno, $errstr, 2 );
  298.     $fp      = $connect;
  299.     $server  = $ip . ":" . $port;
  300.    
  301.     if ( !$connect || !$fp )
  302.         $data = getErr( $ip, $port );
  303.    
  304.    
  305.     else {
  306.  
  307.         fwrite( $connect, $send );
  308.         fwrite( $fp, $send );
  309.         stream_set_timeout( $connect, 2 );
  310.         $output = fread( $fp, 11 );
  311.         $info   = stream_get_meta_data( $connect );
  312.        
  313.         if ( !$output || !isset( $output ) || $output == "" || $info[ 'timed_out' ] )
  314.             $data = getErr( $ip, $port );
  315.        
  316.         else {
  317.             $is_passworded = ord( fread( $fp, 1 ) );
  318.             $plr_count     = ord( fread( $fp, 2 ) );
  319.             $firstval      = dechex( ord( fread( $fp, 1 ) ) );
  320.             $maxplayers    = hexdec( dechex( ord( fread( $fp, 1 ) ) ) . $firstval );
  321.             $max_plrs      = $maxplayers;
  322.             $strlen        = ord( fread( $fp, 4 ) );
  323.             $hostname      = fread( $fp, $strlen );
  324.             $strlen        = ord( fread( $fp, 4 ) );
  325.             $gamemode      = fread( $fp, $strlen );
  326.             $strlen        = ord( fread( $fp, 4 ) );
  327.             $mapname       = fread( $fp, $strlen );
  328.            
  329.             $data = array(
  330.                  "value" => $output,
  331.                 "hostname" => $hostname,
  332.                 "gametype" => $gamemode,
  333.                 "protocol" => "SAMP",
  334.                 "clients" => $plr_count,
  335.                 "maxclients" => $max_plrs,
  336.                 "mapname" => $mapname,
  337.                 "server" => $server,
  338.                 "unclean" => $hostname
  339.             );
  340.         }
  341.     }
  342.         if( $connect )
  343.             fclose( $connect );
  344.        
  345.        
  346.         if ( $_GET[ 'debug' ] == "1" )
  347.             echo "" . print_r( $data ) . "<br><br><big><u>PNG output:</u></big><br><br>";
  348.        
  349.         return $data;
  350. }
  351.  
  352. //Clean input part from their rest behind
  353. function cleanFromRest( $self )
  354. {
  355.     if ( strpos( $self, "\\" ) )
  356.         return substr( $self, 0, strpos( $self, "\\" ) );
  357.     else
  358.         return $self;
  359. }
  360.  
  361. //Get the players
  362. function getPlayers( $input )
  363. {
  364.     $player_str = substr( $input, strpos( $input, "\n" ) + 1, strlen( $input ) );
  365.     $player_str = substr( $player_str, strpos( $player_str, "\n" ) + 1, strlen( $player_str ) );
  366.     $players    = array( );
  367.     $ZOB        = substr_count( $player_str, "\n" );
  368.     $tok        = strtok( $player_str, "\"" );
  369.    
  370.     for ( $k = 1; $k <= $ZOB; $k++ ) {
  371.         $score = substr( $tok, 0, strpos( $tok, " " ) );
  372.        
  373.         if ( substr( $score, 0, 1 ) == " " || substr( $score, 0, 1 ) == "\n" )
  374.             $score = substr( $score, 1, strlen( $score ) );
  375.        
  376.         $ping = substr( $tok, strpos( $tok, " " ) + 1, strlen( $input ) );
  377.        
  378.         if ( substr( $ping, 0, 1 ) == " " || substr( $ping, 0, 1 ) == "\n" )
  379.             $ping = substr( $ping, 0, strlen( $ping ) - 1 );
  380.        
  381.         $tok  = strtok( "\"\n" );
  382.         $name = $tok;
  383.        
  384.         $p_array = array(
  385.              name => $name,
  386.             score => $score,
  387.             ping => $ping
  388.         );
  389.        
  390.         array_push( $players, $p_array );
  391.        
  392.         $tok = strtok( "\"" );
  393.     }
  394.     return $players;
  395. }
  396.  
  397. //Generate the image
  398. function printimage( $data, $root )
  399. {
  400.     $font_size  = 13;
  401.     //Font can't be retrieved remotly, so I need to store it on a local server :(
  402.     $fontpath   = "font.ttf";
  403.     $char_width = 9.5;
  404.    
  405.     if ( isset( $_GET[ 'width' ] ) && $_GET[ 'width' ] != "" && $_GET[ 'width' ] != "no" )
  406.         $image_width = $_GET[ 'width' ];
  407.     else {
  408.         if ( $data[ 'value' ] == "-1" )
  409.         //$image_width = getOfflineWidth();
  410.             $image_width = 400;
  411.         //Automated width detector - multiplies the string length by 10 (approximatly the font width)
  412.         else
  413.             $image_width = 167 + strlen( $data[ 'hostname' ] ) * $char_width;
  414.     }
  415.     //echo $image_width;
  416.     if ( $image_width < 400 )
  417.         $image_width = 400;
  418.    
  419.     $image_width = round( $image_width, 0 );
  420.    
  421.     insertToDatabase( $data, $image_width );
  422.    
  423.     $image_height   = 100;
  424.     $imagecontainer = imagecreatetruecolor( $image_width, $image_height );
  425.    
  426.     imagesavealpha( $imagecontainer, true );
  427.    
  428.     $game     = getGameEngine( $data[ 'protocol' ] );
  429.     $map      = getMapName( $data[ 'mapname' ], $game );
  430.     $gametype = getGametype( $data[ 'gametype' ], $game );
  431.    
  432.     $mappath = $root . "maps/" . $game . "/preview_" . $data[ 'mapname' ] . ".jpg";
  433.    
  434.     if ( $data[ 'value' ] == "-1" )
  435.         $mapimage = imagecreatefromjpeg( $root . "maps/no_response.jpg" );
  436.    
  437.     else if ( check_file( $mappath ) )
  438.         $mapimage = imagecreatefromjpeg( $mappath );
  439.    
  440.     else
  441.         $mapimage = imagecreatefromjpeg( $root . "maps/no_image.jpg" );
  442.    
  443.     if ( ( !isSet( $_GET[ 'color' ] ) || $_GET[ 'color' ] == "no" ) && $data[ 'value' ] != "-1" )
  444.         $bgcolor = AllocateAverageColor( $imagecontainer, $mapimage );
  445.    
  446.     else {
  447.         $html_color = $_GET[ 'color' ];
  448.        
  449.         if ( !isset( $_GET[ 'color' ] ) || $_GET[ 'color' ] == "" ) {
  450.             $html_color = "404040";
  451.            
  452.             if ( strpos( $html_color, "#" ) )
  453.                 $html_color = substr( $html_color, 1 );
  454.            
  455.         }
  456.        
  457.         $bgcolor = ImageColorAllocateFromHex( $imagecontainer, $html_color );
  458.     }
  459.    
  460.     $bg = imagecreatefrompng( $root . "bg.png" );
  461.    
  462.     imagefill( $imagecontainer, 0, 0, $bgcolor );
  463.    
  464.     imagelayereffect( $imagecontainer, IMG_EFFECT_OVERLAY );
  465.    
  466.     imagecopyresampled( $imagecontainer, $bg, 0, 0, 0, 0, $image_width, $image_height, 100, 100 );
  467.    
  468.     imagelayereffect( $imagecontainer, IMG_EFFECT_NORMAL );
  469.    
  470.     //Some colors
  471.     $white = Imagecolorallocate( $imagecontainer, 255, 255, 255 );
  472.     $gray  = Imagecolorallocate( $imagecontainer, 127, 127, 127 );
  473.     $red   = Imagecolorallocate( $imagecontainer, 255, 0, 0 );
  474.    
  475.     //Add preview to the container
  476.     imagecopyresampled( $imagecontainer, $mapimage, 15, 15, 0, 0, 123, 70, imagesx( $mapimage ), imagesy( $mapimage ) );
  477.    
  478.     //Print this if the server is not reachable!
  479.     if ( $data[ 'value' ] == "-1" ) {
  480.         $text = "Server is offline!";
  481.        
  482.         imagettftext( $imagecontainer, $font_size, 0, 150, 30, $red, $fontpath, $text );
  483.        
  484.         //I must add a little watermark :P
  485.         $watermark = imagecreatefrompng( $root . "engine/watermark.png" );
  486.         imagecopyresampled( $imagecontainer, $watermark, $image_width - 75, 60, 0, 0, 63, 35, 320, 176 );
  487.     }
  488.    
  489.     //Print this if it is!
  490.     else {
  491.         $gamepath  = $root . "engine/" . $game . ".PNG";
  492.         $cleanname = $data[ 'hostname' ];
  493.        
  494.         //Print the information onto the picture
  495.         if ( check_file( $gamepath ) ) {
  496.             $gameimage = imagecreatefrompng( $gamepath );
  497.             imagecopyresampled( $imagecontainer, $gameimage, $image_width - 75, 60, 0, 0, 63, 35, 320, 176 );
  498.         }
  499.        
  500.         //Colored hostname
  501.         $length = 150;
  502.         $color  = $white;
  503.         $maxlen = strlen( $data[ 'unclean' ] );
  504.         $dots   = false;
  505.        
  506.         if ( $_GET[ 'width' ] != "" && isset( $_GET[ 'width' ] ) && ( 167 + strlen( $data[ 'hostname' ] ) * $char_width ) > $_GET[ 'width' ] ) {
  507.             $dots = true;
  508.             $maxlen -= round( ( ( 195 + strlen( $data[ 'hostname' ] ) * $char_width ) - floatval( $_GET[ 'width' ] ) ) / $char_width, 0 ) + 3;
  509.         }
  510.        
  511.         for ( $i = 0; $i <= $maxlen; $i++ ) {
  512.             if ( $data[ 'unclean' ][ $i ] == "^" && $_GET["game"] != "SAMP" ) {
  513.                 $tempcolor = getColorfromNumber( $data[ 'unclean' ][ $i + 1 ], $imagecontainer );
  514.                 if ( $tempcolor == "-1" ) {
  515.                     imagettftext( $imagecontainer, $font_size, 0, $length, 30, $color, $fontpath, $data[ 'unclean' ][ $i ] );
  516.                     $length += $char_width;
  517.                 }
  518.                
  519.                 else {
  520.                     $color = $tempcolor;
  521.                     $i++;
  522.                 }
  523.             }
  524.            
  525.             else {
  526.                 imagettftext( $imagecontainer, $font_size, 0, $length, 30, $color, $fontpath, $data[ 'unclean' ][ $i ] );
  527.                 $length += $char_width;
  528.             }
  529.         }
  530.        
  531.         if ( $dots )
  532.             imagettftext( $imagecontainer, $font_size, 0, $length, 30, $white, $fontpath, "..." );
  533.     }
  534.    
  535.     $mapshadow = imagecreatefrompng( $root . "maps/shadow.png" );
  536.     imagecopyresampled( $imagecontainer, $mapshadow, 15, 15, 0, 0, 126, 73, 334, 194 );
  537.     imagettftext( $imagecontainer, 10, 0, 150, 47, $white, $fontpath, "IP: {$data[ 'server' ]}\nMap: {$map}\nGametype: " . strtoupper( $gametype ) . "\nPlayers: {$data[ 'clients' ]}/{$data[ 'maxclients' ]}" );
  538.    
  539.     //Render the final picture
  540.     imagepng( $imagecontainer );
  541.     imagedestroy( $imagecontainer );
  542. }
  543.  
  544. function getColorfromNumber( $number, $imagecontainer )
  545. {
  546.     switch ( floatval( $number ) ) {
  547.         case ( 0 ):
  548.             return Imagecolorallocate( $imagecontainer, 0, 0, 0 );
  549.             break;
  550.        
  551.         case ( 1 ):
  552.             return Imagecolorallocate( $imagecontainer, 255, 0, 0 );
  553.             break;
  554.        
  555.         case ( 2 ):
  556.             return Imagecolorallocate( $imagecontainer, 0, 255, 0 );
  557.             break;
  558.        
  559.         case ( 3 ):
  560.             return Imagecolorallocate( $imagecontainer, 255, 255, 0 );
  561.             break;
  562.        
  563.         case ( 4 ):
  564.             return Imagecolorallocate( $imagecontainer, 0, 0, 255 );
  565.             break;
  566.        
  567.         case ( 5 ):
  568.             return Imagecolorallocate( $imagecontainer, 0, 255, 255 );
  569.             break;
  570.        
  571.         case ( 6 ):
  572.             return Imagecolorallocate( $imagecontainer, 255, 0, 255 );
  573.             break;
  574.        
  575.         case ( 7 ):
  576.             return Imagecolorallocate( $imagecontainer, 255, 255, 255 );
  577.             break;
  578.        
  579.         case ( 8 ):
  580.             return Imagecolorallocate( $imagecontainer, 204, 153, 51 );
  581.             break;
  582.        
  583.         case ( 9 ):
  584.             return Imagecolorallocate( $imagecontainer, 141, 141, 141 );
  585.             break;
  586.        
  587.         default:
  588.             return "-1";
  589.     }
  590. }
  591.  
  592. //Get the game version
  593. function getGameEngine( $var )
  594. {
  595.     if ( $_GET[ "game" ] == "SAMP" )
  596.         return "SAMP";
  597.    
  598.     else if ( $_GET[ "game" ] == "MC" )
  599.         return "Minecraft";
  600.    
  601.    
  602.     switch ( $var ) {
  603.         case ( "5" ):
  604.         case ( "6" ):
  605.             return "MW1"; // IW3 engine
  606.             break;
  607.        
  608.         case ( "101" ):
  609.             return "WAW"; // IW3 engine
  610.             break;
  611.        
  612.         case ( "118" ):
  613.             return "COD2";
  614.             break;
  615.        
  616.         case ( "142" ): // (g)a(y)Rev protocol (info query)
  617.         case ( "144" ): // (g)a(y)Rev protocol (status query)
  618.         case ( "61586" ):
  619.             return "MW2"; // IW4 engine
  620.             break;
  621.        
  622.         case ( "104" ):
  623.         case ( "19816" ):
  624.             return "MW3"; // IW5 engine
  625.             break;
  626.        
  627.         default:
  628.             return $var;
  629.             break;
  630.     }
  631. }
  632.  
  633. //Get better GT names
  634. function getGametype( $var, $game )
  635. {
  636.     switch ( $var ) {
  637.         case ( "war" ):
  638.             if ( $game == "WAW" ) //WAW gametype war is not IW's TDM
  639.                 return "WAR";
  640.             else
  641.                 return "TDM";
  642.             break;
  643.        
  644.         case ( "dm" ):
  645.             return "FFA";
  646.             break;
  647.        
  648.         case ( "dd" ):
  649.             return "DEM";
  650.             break;
  651.        
  652.         case ( "sd" ):
  653.             return "S&D";
  654.             break;
  655.        
  656.         default:
  657.             return $var;
  658.             break;
  659.            
  660.     }
  661. }
  662.  
  663. //Get the localized mapname
  664. function getMapName( $var, $game )
  665. {
  666.     if ( $fp = fopen( 'http://momo5504.square7.de/banner_stuff/getMap.php?mapname=' . $var . '&game=' . $game, 'r' ) ) {
  667.         $content = '';
  668.        
  669.         while ( $line = fgets( $fp, 1024 ) ) {
  670.             $content .= $line;
  671.         }
  672.        
  673.         if ( $content == "" )
  674.             return $var;
  675.         else {
  676.             if ( strpos( $content, "\n" ) )
  677.                 return substr( $content, 0, strpos( $content, "\n" ) );
  678.             else
  679.                 return $content;
  680.         }
  681.     } else
  682.         return $var;
  683. }
  684.  
  685.  
  686. //Checks if a file exists and returns boolean true or false
  687. function check_file( $file )
  688. {
  689.     if ( substr( $file, 0, 7 ) == "http://" ) {
  690.         $file_headers = @get_headers( $file );
  691.         if ( $file_headers[ 0 ] == 'HTTP/1.1 404 Not Found' )
  692.             return false;
  693.         else
  694.             return true;
  695.     } else
  696.         return file_exists( $file );
  697. }
  698.  
  699. //Allocates a color from a HEX color ( e.g. #00FF4B )
  700. function ImageColorAllocateFromHex( $img, $hexstr )
  701. {
  702.     $int = hexdec( $hexstr );
  703.    
  704.     return ImageColorAllocate( $img, 0xFF & ( $int >> 0x10 ), 0xFF & ( $int >> 0x8 ), 0xFF & $int );
  705. }
  706.  
  707. //Allocates the average color of the given image (all colors / amount)
  708. function AllocateAverageColor( $img, $i )
  709. {
  710.     $max_x = imagesx( $i );
  711.     $max_y = imagesy( $i );
  712.    
  713.     for ( $x = 0; $x < $max_x; $x++ ) {
  714.         for ( $y = 0; $y < $max_y; $y++ ) {
  715.             $rgb = imagecolorat( $i, $x, $y );
  716.            
  717.             $rTotal += ( $rgb >> 16 ) & 0xFF;
  718.             $gTotal += ( $rgb >> 8 ) & 0xFF;
  719.             $bTotal += $rgb & 0xFF;
  720.         }
  721.     }
  722.    
  723.     $total = $max_x * $max_y;
  724.    
  725.     $rAverage = round( $rTotal / $total );
  726.     $gAverage = round( $gTotal / $total );
  727.     $bAverage = round( $bTotal / $total );
  728.    
  729.     return ImageColorAllocate( $img, $rAverage, $gAverage, $bAverage );
  730. }
  731.  
  732. //Allocates the most dominant color of a given image
  733. function AllocateDominantColor( $img, $i )
  734. {
  735.     $colours = array( );
  736.     $index   = array( );
  737.     for ( $x = 1; $x < imagesx( $i ); $x++ ) {
  738.         for ( $y = 1; $y < imagesy( $i ); $y++ ) {
  739.             $int = imagecolorat( $i, $x, $y );
  740.             $key = ( 0xFF & ( $int >> 0x10 ) ) . " " . ( 0xFF & ( $int >> 0x8 ) ) . " " . ( 0xFF & $int );
  741.             if ( !isSet( $colours[ $key ] ) ) {
  742.                 $colours[ $key ] = 1;
  743.             } else {
  744.                 $colours[ $key ]++;
  745.             }
  746.         }
  747.     }
  748.     arsort( $colours, SORT_NUMERIC );
  749.    
  750.     $r = substr( key( $colours ), 0, strpos( key( $colours ), " " ) );
  751.     $g = substr( substr( key( $colours ), strpos( key( $colours ), " " ) + 1 ), 0, strpos( key( $colours ), " " ) );
  752.     $b = substr( substr( substr( key( $colours ), strpos( key( $colours ), " " ) + 1 ), strpos( key( $colours ), " " ) + 1 ), 0, strpos( key( $colours ), " " ) );
  753.    
  754.     return ImageColorAllocate( $img, $r, $g, $b );
  755. }
  756.  
  757. //Allocates a random color in a given image
  758. function AllocateRandColor( $img, $i )
  759. {
  760.     $x   = rand() % imagesx( $i );
  761.     $y   = rand() % imagesy( $i );
  762.     $rgb = imagecolorat( $i, $x, $y );
  763.    
  764.     return $rgb;
  765. }
  766. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement