<?php
    
    
//Get server ip/port
    
$ipp $_GET['ipp'];
    
$stripp $_GET['stripp'];
    
$usercolor $_GET['usercolor'];
    
$callname 'http://www.guiddb.com/api/get_server_info.php?server=' $ipp;
    
$result file($callname);
    
$results unserialize($result[0]);
    
    
//Include tga file handler
    
include 'tgafilehandler.php';
    
    
//Tell the browser its a jpeg
    
header('Content-type: image/jpeg');
    
    
//Set mapname for banner
    
$mapname $results['mapname'];
    
    
//Set banner file to start with
    
$bannerfile 'images/' $mapname '.jpg';
    
    
//Check for banner, if non exist try to create
    
if (!file_exists($bannerfile)) {
        
        
//Set all the vars needed for generated the banner
        
$pk3file 'q3ut4/' $mapname '.pk3';
        
$lvlshot 'levelshots/' $mapname '.jpg';
        
$baseimagename 'images/levelshots/' $mapname;
        
$tgafile 'images/levelshots/' $mapname '.tga';
        
$outjpeg 'images/levelshots/' $mapname '.jpg';
        
        
//Open the pk3 and get the levelshot
        
$zip = new ZipArchive;
        
$res $zip->open($pk3file);
        if (
$res === TRUE) {
            if (!
$zip->extractTo('images/'$lvlshot) === TRUE) {
                
$lvlshot 'levelshots/' $mapname '.tga';
                
$zip->extractTo('images/'$lvlshot);
            }
            
$zip->close();
        }
        
        
//If there is no jpg but a tga, *try* and convert it, doesnt work very well.
        
if (!file_exists($outjpeg) && file_exists($tgafile)) {
            
            
tga2jpg ("$baseimagename);
            
// Return the resource image alone
            //$resource_image = imagecreatefromtga ( "$tgafile" );

            // Declare the content-type as a JPEG image.
            //header ( 'Content-type: image/jpeg' );

            // Convert the image to JPEG for smaller file size and compatability
            //imagejpeg ( $resource_image, $outjpeg, 100 );
            
            //Close image
            //imagedestroy ( $resource_image );
        
}
        
        
//If no jpeg exists as of now set the banner to default, else make banner from levelshot
        
if (!file_exists($outjpeg)) {
            
$bannerfile 'images/banner-blank.jpg';
        } 
        else {
            
            
//Load levelshot jpeg
            
$lvlshotimage imageCreateFromJPEG($outjpeg);
            
            
//Get x for levelshot
            
$lvlshottx imagesx($lvlshotimage);
            
            
//Get y for levelshot
            
$lvlshotty imagesy($lvlshotimage);
            
            
//Set y offset for crop
            
$lvlshotsy $lvlshotty .352;
            
            
//Set size of y crop
            
$lvlshotly $lvlshotty .229;
            
            
//Create new image for the map banner
            
$bannerout imagecreatetruecolor46880 );
            
            
//Turn on AA for images
            //imageantialias($lvlshotimage, true);
            //imageantialias($bannerout, true);
                
            //Set jpeg header info
            
header 'Content-type: image/jpeg' );
            
            
//Copy and resize from levelshot to new banner
            
imagecopyresized($bannerout$lvlshotimage000$lvlshotsy46880$lvlshottx$lvlshotly);
            
            
//Output jpeg to bannerfile for map
            
imagejpeg($bannerout$bannerfile100);
            
            
//Close both images
            
imagedestroy ($bannerout);
            
imagedestroy ($lvlshotimage);
        }
    }
    
    
//Create the base image from the default banner
    
$preimage imageCreateFromJPEG($bannerfile);
    
$superimp imagecreatetruecolor46880 );

    
$precolors = array(
        
'black' => imagecolorallocate($superimp000),
        
'white' => imageColorAllocate($superimp255255255),
        
'grey' => imageColorAllocate($superimp128128128),
    );
    
    
//Turn on anti-aliasing functions for the image
    //imageantialias($preimage, true);
    //imageantialias($superimp, true);
    
    
imagefill($superimp00$precolors['grey']);
    
    
imagefilledrectangle($superimp552575$precolors['black']);
    
imagefilledrectangle($superimp30346327$precolors['white']);
    
imagefilledrectangle($superimp303024553$precolors['white']);
    
imagefilledrectangle($superimp2503046353$precolors['white']);
    
    
imagecopymerge($preimage$superimp00004688033);
    
    
imageJPEG($preimage'images/current-render.jpg'100);
    
    
imageDestroy($preimage);
    
imageDestroy($superimp);
    
    
$image imageCreateFromJPEG('images/current-render.jpg');
    
//imageantialias($image, true);
    
    
    //Define colors to be used in image
    
$colors = array(
        
'grey' => imagecolorallocate($image128128128),
        
'black' => imageColorAllocate($image000),
        
'darkred' => imageColorAllocate($image17000),
        
'red' => imageColorAllocate($image25500),
        
'green' => imageColorAllocate($image02550),
        
'brightgreen' => imageColorAllocate($image5025543),
        
'darkgrey' => imageColorAllocate($image363636),
        
'blue' => imageColorAllocate($image00255),
        
'yellow' => imageColorAllocate($image2552550),
        
'brightblue' => imageColorAllocate($image0255255),
        
'purple' => imageColorAllocate($image2550255),
        
'white' => imageColorAllocate($image255255255),
    );

    
    if (!
$usercolor) {
        
$usercolor 'brightgreen';
    }
    
    
//Set font to be used for the static text
    
$font './gothicb.ttf';
    
    
//Set static text vars
    
$maptext 'Map :';
    
$nametext 'Server name :';
    
$iptext 'IP :';
    
$playertext 'Players :';
    
$aliastext "Collected Alias's :";
    
    
//Set all active text vars
    
$sv $results['hostname_c'];
    
$servertext $ipp;
    
$numalias $results['aliases'];       
    
$servername substr($sv035);
    
    
//Explode sv_hostname at any color codes
    
$servernamepieces explode("^"$servername);
    for(
$i 0$size sizeof($servernamepieces); $i $size; ++$i)
    {
        
//Get color code
        
$colorcode substr($servernamepieces[$i], 01);
        
        
//Determine color
        
switch ($colorcode) {
            case 
0:
                
$servernamecolors[$i] = 'white';
                break;
            case 
1:
                
$servernamecolors[$i] = 'red';
                break;
            case 
2:
                
$servernamecolors[$i] = 'green';
                break;
            case 
3:
                
$servernamecolors[$i] = 'yellow';
                break;
            case 
4:
                
$servernamecolors[$i] = 'blue';
                break;
            case 
5:
                
$servernamecolors[$i] = 'brightblue';
                break;
            case 
6:
                
$servernamecolors[$i] = 'purple';
                break;
            case 
7:
                
$servernamecolors[$i] = 'black';
                break;
        }
        
$strp strpos($sv'^');
        
//Strip color code from segment
        
if ($strp == false) {
            
$servernamepiece[$i] = substr($servernamepieces[$i], 1);
        } else {
            
$servernamepiece[$i] = substr($servernamepieces[$i], 0);
        }
    }
    
    
    
//Math for the player meter
    
$totalplayers $results['nowplaying'];
    
$totalslots $results['maxclients'];
    
$percentplayers = ($totalplayers $totalslots);
    
$totalgraph = (+ (($percentplayers) * 66));
    
$numplayers $totalplayers ' / ' $totalslots;
        
    
//gaussian blur matrix
    
$gaussian = array(array(1.02.01.0), array(2.04.02.0), array(1.02.01.0));
    
    
//Background for player meter

    //Output phase 1 of static text, white blurred background
    
imagettftext($image1603622$colors[$usercolor], $font$nametext);
    
imagettftext($image1603523$colors[$usercolor], $font$nametext);
    
imagettftext($image1603422$colors[$usercolor], $font$nametext);
    
imagettftext($image1603521$colors[$usercolor], $font$nametext);
    
    
imagettftext($image1603550$colors[$usercolor], $font$iptext);
    
imagettftext($image1603649$colors[$usercolor], $font$iptext);
    
imagettftext($image1603449$colors[$usercolor], $font$iptext);
    
imagettftext($image1603548$colors[$usercolor], $font$iptext);
    
    
imagettftext($image16026649$colors[$usercolor], $font$playertext);
    
imagettftext($image16026550$colors[$usercolor], $font$playertext);
    
imagettftext($image16026548$colors[$usercolor], $font$playertext);
    
imagettftext($image16026449$colors[$usercolor], $font$playertext);
    
    
imagettftext($image1203573$colors[$usercolor], $font$aliastext);
    
imagettftext($image1203672$colors[$usercolor], $font$aliastext);
    
imagettftext($image1203472$colors[$usercolor], $font$aliastext);
    
imagettftext($image1203571$colors[$usercolor], $font$aliastext);
    
    
imagettftext($image12026672$colors[$usercolor], $font$maptext);
    
imagettftext($image12026573$colors[$usercolor], $font$maptext);
    
imagettftext($image12026571$colors[$usercolor], $font$maptext);
    
imagettftext($image12026472$colors[$usercolor], $font$maptext);
    
    
//Output the backdrop of the server name in white
    
$sx 180;
    for(
$i 0$size sizeof($servernamepiece); $i $size; ++$i)
    {
    
        
//Set color for segment
        
if ($stripp == true) {
            
$color 'black';
        } else {
            
$color $servernamecolors[$i];
        }
        
        
//Get font bounding box info
        
$bbox imagettfbbox(130$font$servernamepiece[$i]);
        
        
//Set the lower left x cordinate with starting offset
        
$x $bbox[0] + $sx;
        
        
//Draw this piece of sv_hostname backdrop with color
        
imagettftext($image130$x23$colors[$color], $font$servernamepiece[$i]);
        
imagettftext($image130, ($x 1), 22$colors[$color], $font$servernamepiece[$i]);
        
imagettftext($image130, ($x 1), 22$colors[$color], $font$servernamepiece[$i]);
        
imagettftext($image130$x21$colors[$color], $font$servernamepiece[$i]);
        
        
//Set the new starting  offset for next piece of sv_hostname by
        //adding the lower right x cordinate to current offset
        
$sx $sx $bbox[2] + 2;
    }
    
    
//Server ip/port all black
    
imagettftext($image1207250$colors['black'], $font$servertext);
    
imagettftext($image1207349$colors['black'], $font$servertext);
    
imagettftext($image1207149$colors['black'], $font$servertext);
    
imagettftext($image1207248$colors['black'], $font$servertext);
    
    
//Number of active players
    
imagettftext($image12035750$colors['black'], $font$numplayers);
    
imagettftext($image12035849$colors['black'], $font$numplayers);
    
imagettftext($image12035649$colors['black'], $font$numplayers);
    
imagettftext($image12035748$colors['black'], $font$numplayers);
    
    
//Number of alias's found on server
    
imagettftext($image10018872$colors['black'], $font$numalias);
    
imagettftext($image10018773$colors['black'], $font$numalias);
    
imagettftext($image10018771$colors['black'], $font$numalias);
    
imagettftext($image10018672$colors['black'], $font$numalias);
    
    
//Map name currently being played on server
    
imagettftext($image10031872$colors['black'], $font$mapname);
    
imagettftext($image10031773$colors['black'], $font$mapname);
    
imagettftext($image10031771$colors['black'], $font$mapname);
    
imagettftext($image10031672$colors['black'], $font$mapname);
    
    
//Do gaussian blur
    //imageconvolution($image, $gaussian, 16, 0);
    
    //Player meter
    
imagefilledrectangle($image7$totalgraph2373$colors[$usercolor]);

    
//Output all active text
    
    //Output sv_hostname in color
    
$sx 180;
    for(
$i 0$size sizeof($servernamepiece); $i $size; ++$i)
    {
        
        
//Get font bounding box info...
        
$bbox imagettfbbox(130$font$servernamepiece[$i]);
        
        
//Set font x position
        
$x $bbox[0] + $sx;
        
        
//Draw this segment of sv_hostname in all black
        
imagettftext($image130$x22$colors['white'], $font$servernamepiece[$i]);
        
        
//Set new starting offset
        
$sx $sx $bbox[2] + 2;
    }

    
//Server ip/port all black
    
imagettftext($image1207249$colors['white'], $font$servertext);
    
    
//Number of active players
    
imagettftext($image12035749$colors['white'], $font$numplayers);
    
    
//Number of alias's found on server
    
imagettftext($image10018772$colors['white'], $font$numalias);
    
    
//Map currently being played on server
    
imagettftext($image10031772$colors['white'], $font$mapname);
    
    
//Output phase 2 of static text, red post-blur face
    
imagettftext($image1603522$colors['black'], $font$nametext);
    
imagettftext($image1603549$colors['black'], $font$iptext);
    
imagettftext($image16026549$colors['black'], $font$playertext);
    
imagettftext($image1203572$colors['black'], $font$aliastext);
    
imagettftext($image12026572$colors['black'], $font$maptext);
    
    
//Output image to browser
    
imageJPEG($imageNULL100);
    
    
//Destroy image to clear memory.
    
imageDestroy($image);
    
?>