Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

PHP Stat Cards


dg1423
 Share

Recommended Posts

well people have been asking how, so here is a tut…..

Things you will need:

* Card Background(s) (a sample is provided below)
* Basic understanding of PHP; or at least the primary fundamentals of coding
* Modified Sprite and Item files (samples provided below)
* A webhost with PHP and PHP sockets enabled

Here is a sample card background:

![](http://stwalarts.game-host.org/wtg/card2.gif)

PLEASE NOTE: This background was created by ICT/Atrophy for the sole purpose of my use. Spainion simple recolored it for his game Honor of Blood. I suggest you come up with an original design and layout (instructions on moving tags will be below).

Here are examples of modded sprite and item files for use in this script:

Sprites:

http://stwalarts.game-host.org/wtg/Sprites2.gif

Items:

http://stwalarts.game-host.org/wtg/Items2.gif

PLEASE NOTE: You will only need the items file if your game uses PD. PLEASE REMEMBER that you will need to modify the sprite and item files for your game the way shown so that the sprites will be the same as those ingame. ALSO, because of the PD formula I used, it is required that you leave the icon section at the top of the item file if you use it, otherwise it'll mess up.

okay, so here's the entire script. I will then break it down into parts explaining the function on the script.

```
3) {
$im  = imagecreatetruecolor(150, 30); /* Create a blank image */
      $bgc = imagecolorallocate($im, 255, 255, 255);
                      $tc  = imagecolorallocate($im, 0, 0, 0);
                    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
                    /* Output an errmsg */
                    imagestring($im, 1, 5, 5, "Invalid card number!", $tc);
imagegif($im);
        }else{
parse_str($out);

parse_str($statout);

$card = loadgif("card" . $_GET['card'] . ".gif");

$black = ImageColorAllocate($card, 255, 255, 255);

ImageString($card, 1, 42, 20, "Name: " . $name . "", $black);
ImageString($card, 1, 42, 30, "Guild: " . $Guild . "", $black);
ImageString($card, 1, 42, 40, "Class: " . $class . "", $black);
ImageString($card, 1, 42, 50, "Lvl:" . $level . "", $black);

$levelen = strlen("Lvl: " . $level . "") * 3;

ImageString($card, 1, 42 + $levelen + 22, 50, "Sex: " . $sex . "", $black);
ImageString($card, 1, 42, 60, "HP:" . $HP . "", $black);
ImageString($card, 1, 92, 60, "MP:" . $MP . "", $black);
ImageString($card, 1, 68, 70, "SP:" . $SP . "", $black);
ImageString($card, 1, 42, 80, "STR:" . $STR . "", $black);
ImageString($card, 1, 92, 80, "DEF:" . $DEF . "", $black);
ImageString($card, 1, 42, 90, "MAGI:" . $MAGI . "", $black);
ImageString($card, 1, 92, 90, "SPEED:" . $SPEED . "", $black);
$ston = stripos($status, "On");
if ($ston === 0)
{
      $offcolor = ImageColorAllocate($card, 62, 137, 84);
      imagefilledrectangle($card, 17, 83, 26, 93, $offcolor);
}
else
{
      $oncolor = ImageColorAllocate($card, 142, 51, 51);
      imagefilledrectangle($card, 17, 83, 26, 93, $oncolor);
}
$sprite_sheet = loadgif("Sprites2.gif");
$charsprite = ImageCreate(32, 64);

$bg  = imagecolorallocate($charsprite, 0, 0, 0);
imagecolortransparent ($charsprite, $bg);
imagefill($charsprite, 0, 0, $bg);
imagecolortransparent ($card, $bg);
imagecolortransparent ($sprite_sheet, $bg);
imagecopyresized($charsprite, $sprite_sheet, 0, 0, 0, $sprite_y, 32, 64, 32, 64);
Imagedestroy($sprite_sheet);
$item_sheet = Loadgif("Items2.gif");
imagecolortransparent ($item_sheet, $bg);
if ($legs_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $legs_y,  32, 64, 32, 64);
}
if ($armor_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $armor_y,  32, 64, 32, 64);
}
if ($helmet_y != "0")
{
            imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $helmet_y,  32, 64, 32, 64);
}
if ($necklace_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $necklace_y,  32, 64, 32, 64);
}
if ($ring_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $ring_y,  32, 64, 32, 64);
}
if ($shield_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $shield_y,  32, 64, 32, 64);
}
if ($weapon_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $weapon_y,  32, 64, 32, 64);
}
imagecopyresized($card, $charsprite, 7, 11, 0, 0, 32, 64, 32, 64);
imagedestroy($charsprite);
imagedestroy($item_sheet);
}
      }
}
imagegif($card);
?>
```
alright, lets get this party started:

```

starts the script and tells the browser its an image

```
function LoadGIF($imgname)
{
    $im = @imagecreatefromgif($imgname); /* Attempt to open */
    if (!$im) { /* See if it failed */
        $im  = imagecreatetruecolor(150, 30); /* Create a blank image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        /* Output an errmsg */
        imagestring($im, 1, 5, 5, "Error loading $imgname", $tc);
    }
    return $im;
}
```
this is the gif loading function, please remember that the images MUST be gifs in this script (to save loading time and space) you can modify the script to load PNGs but I recommend leaving it at gifs.

```
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
or die("could not connect");
$address = gethostbyname('stwalarts.game-host.org');
$service_port = 4001;
$result = socket_connect($socket, $address, $service_port);
```
this is the socket connector, you have to change "stwalarts.game-host.org" to the ip of your server and 4001 to the port.

```
if ($result == false){
$card  = imagecreatetruecolor(120, 20); /* Create a blank image */
        $backcol = imagecolorallocate($card, 255, 255, 255);
        $textol  = imagecolorallocate($card, 0, 0, 0);
        imagefilledrectangle($card, 0, 0, 150, 30, $backcol);
        /* Output an errmsg */
        imagestring($card, 1, 5, 5, "The server is offline!", $textcol);
}else{
```
this checks if the server is offline and creates a small image that says "The server is Offline!", can be easily modified

```
$in = "getplayerspriteinfo,".$_GET['name'];
socket_write($socket, $in, strlen($in));
$out = socket_read($socket, 2048);
$in = "getplayerstats,".$_GET['name'];
socket_write($socket, $in, strlen($in));
$statout = socket_read($socket, 2048);
socket_close($socket);
```
this sends and recieves the character data from the server

```
if (stripos($statout, "name") === False){
        $im  = imagecreatetruecolor(150, 30); /* Create a blank image */
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
      /* Output an errmsg */
        imagestring($im, 1, 5, 5, $statout, $tc);
      imagegif($im);
}else{
```
this checks the server's response for integrity and to see if the character exists or not (i.e: if character dg1423 does not exist it will return a small image displaying "Could not find dg1423")

```
if ($_GET['card'] > 3) {
$im  = imagecreatetruecolor(150, 30); /* Create a blank image */
      $bgc = imagecolorallocate($im, 255, 255, 255);
                      $tc  = imagecolorallocate($im, 0, 0, 0);
                    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
                    /* Output an errmsg */
                    imagestring($im, 1, 5, 5, "Invalid card number!", $tc);
imagegif($im);
        }else{
```
this is for if you have multiple backgrounds, it checks the card number to make sure it's valid.
NOTE: in "if ($_GET['card'] > 3) {" the 3 represents the max number of backgrounds. I suggest you change it if you have more/less backgrounds.

```
parse_str($out);

parse_str($statout);
```
this parses the information recieved from the server

```
$card = loadgif("card" . $_GET['card'] . ".gif");
```
loads the card background from the card number given (i.e: if card=2 in the url the script will load card2.gif)

```
$black = ImageColorAllocate($card, 255, 255, 255);
```
this creates the color for the text, I originally had it black (thus the reason it's call $black) but its actually white, can easily be changed ( 255, 255, 255 is the RGB value you would like as the color)

```
ImageString($card, 1, 42, 20, "Name: " . $name . "", $black);
ImageString($card, 1, 42, 30, "Guild: " . $Guild . "", $black);
ImageString($card, 1, 42, 40, "Class: " . $class . "", $black);
ImageString($card, 1, 42, 50, "Lvl:" . $level . "", $black);
```
writes that info to the card, format is (IMAGE, FONTSIZE(1-5), X, Y, TEXT, COLOR)
in the first case $card represents the image of the background, it uses font size 1, writes to the co-ordinates (42, 30), shows "Name: dg1423" (if it were looking for dg1423), and in the color stored in $black.

```
$levelen = strlen("Lvl: " . $level . "") * 3;
```
that calculates the end of the level text so as to write the gender after it.

```
ImageString($card, 1, 42 + $levelen + 22, 50, "Sex: " . $sex . "", $black);
```
writes the gender after the level (i.e: "Lvl: 6    Sex: Male")

```
ImageString($card, 1, 42, 60, "HP:" . $HP . "", $black);
ImageString($card, 1, 92, 60, "MP:" . $MP . "", $black);
ImageString($card, 1, 68, 70, "SP:" . $SP . "", $black);
ImageString($card, 1, 42, 80, "STR:" . $STR . "", $black);
ImageString($card, 1, 92, 80, "DEF:" . $DEF . "", $black);
ImageString($card, 1, 42, 90, "MAGI:" . $MAGI . "", $black);
ImageString($card, 1, 92, 90, "SPEED:" . $SPEED . "", $black);
```
writes more info to the card

```
$ston = stripos($status, "On");
if ($ston === 0)
{
      $offcolor = ImageColorAllocate($card, 62, 137, 84);
      imagefilledrectangle($card, 17, 83, 26, 93, $offcolor);
}
else
{
      $oncolor = ImageColorAllocate($card, 142, 51, 51);
      imagefilledrectangle($card, 17, 83, 26, 93, $oncolor);
}
```
Determines the on/offline status of the player and creates a small square on the card (red if offline, green if on) format is the same for color, but for the square it's (IMAGE, UPPERX, UPPERY, LOWERX, LOWERY, COLOR) the upper x and y are the upper-left co-ordinates of the square, the lower x and y are the co-ordinates of the bottom-right corner.

```
$sprite_sheet = loadgif("Sprites2.gif");
$charsprite = ImageCreate(32, 64);
```
this loads Sprites2.gif and creates a 32x64 blank space to put the character and PD

```
$bg  = imagecolorallocate($charsprite, 0, 0, 0);
imagecolortransparent ($charsprite, $bg);
imagefill($charsprite, 0, 0, $bg);
imagecolortransparent ($card, $bg);
imagecolortransparent ($sprite_sheet, $bg);
```
in order: creates the color black (0, 0, 0), sets black as transparent on the 32x64 char space, fills the 32x64 charspace with black(now transparent), makes black transparent on the card background and makes black transparent on the sprite sheet.

```
imagecopyresized($charsprite, $sprite_sheet, 0, 0, 0, $sprite_y, 32, 64, 32, 64);
Imagedestroy($sprite_sheet);
```
copies the player's sprite from the sprite sheet to the character space and destroys the sheet (saves memory space, destroys as in remove from memory, not deleting the file)

```
$item_sheet = Loadgif("Items2.gif");
imagecolortransparent ($item_sheet, $bg);
```
loads Items2.gif and sets black transparent, can be removed if you don't have PD

```
if ($legs_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $legs_y,  32, 64, 32, 64);
}
if ($armor_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $armor_y,  32, 64, 32, 64);
}
if ($helmet_y != "0")
{
            imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $helmet_y,  32, 64, 32, 64);
}
if ($necklace_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $necklace_y,  32, 64, 32, 64);
}
if ($ring_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $ring_y,  32, 64, 32, 64);
}
if ($shield_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $shield_y,  32, 64, 32, 64);
}
if ($weapon_y != "0")
{
      imagecopyresized($charsprite, $item_sheet, 0, 0, 0, $weapon_y,  32, 64, 32, 64);
}
```
the PD algorithm (prolly shouldn't call it an algorithm as it's not THAT complicated), can be removed if you don't have PD.

```
imagecopyresized($card, $charsprite, 7, 11, 0, 0, 32, 64, 32, 64);
imagedestroy($charsprite);
imagedestroy($item_sheet);
}
      }
}
```
copies the sprite from the character space and puts it on the card background and then destroys the character space and item sheet.

```
imagegif($card);
?>
```
displays the final product.

I think I did a pretty good job explaining that. at least I hope so

```
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...