[4. Writing the main script]
This is the principal and longest part of the tutorial.

Create a file on your PC named "
generate.php". Open it (e.g. with notepad) and prepare for editing.
I will explain the code as we write in the file. Just copy / paste the code sections into the file.
Code:
<?php
$im = imagecreatefrompng("images/signature.png");
imageantialias($im, true);
imageSaveAlpha($im, true);
imagealphablending($im , false);
# creating a variable that stores the image from the prepared signature we uploaded to the server.
# imageantialias, imageSaveAlpha and imagealphablending are png related settings that I am unsure what they do

; they are necesary for they make the signature to have alpha transparency (for example my signatures have cut corners that are transparent, whereas were they jpg that corner would have been filled with a color).
Code:
$site = file_get_contents("http://www.twilightmuonline.com/search.php?chara=zukih28s");
# using the search function from the twilight website we search for the character whom status we want to show on the signature. replace "
zukih28s" with the character name you chose the signature to be for.
# the character name we send to the search function does
NOT require to be cAsE SeNsItIvE (unlike in-game) so don't worry about it.
# it's good practice to test the link between
" " and make sure it works before continuing.
# file_get_contents() gives us the HTML page (with the results of the search on it) which gets stored in the
$site variable.
Code:
preg_match_all("/<strong>(.*)<\/strong>/", $site, $results0);
# preg_match_all() is a php function that takes two arguments (here,
<strong>(.*)<\/strong> and
$site), compares them, and puts what it finds in an array (here,
$results0).
# sytax:
(.*) = any "something".
# $results0 array will look like this after the above code is executed:
# as you see, what this code line did was to search in the HTML code (of the
$site) for anything matching "
<strong>ANYTHING </strong>". Here, the findings (two matches were found) were put in a matrix (or two-dimensional array), in the [0] it was put with the searched criteria included, and in [1] it was put without the search criteria.
# note that
[1][0] has the value of the on-line status.
# if you check the HTML code of the web page
http://www.twilightmuonline.com/sear...chara=zukih28s you will see that it contains
Code:
<td bgcolor=black width=30>
<font face=Arial size=2>
<font color='#00FF00'>
<strong>Online</strong>
</font>
</font>
</td>
The code is very messy in original, I indented it here for easy reading (also while working on this script I discovered some mistakes in the twilight's website HTML code

but I didn't check any further anyway; if it works ... ).
# if you are wondering why we put "
/<strong>(.*)<\/strong>/" instead of "
<strong>(.*)</strong>" it's because, besides that the arguments must be contained between
"/ /", some characters need to be escaped. "
\" is used for escaping characters. If those characters aren't escaped the php compiler misinterprets what we are trying to do and will give errors.
Code:
preg_match_all("/<td bgcolor=black width=30><font face=Arial size=2 font color=white>(.*)\((.*)\)\[(.*)\]<\/b><\/font><\/td>/", $site, $results1);
# here we have 3 readings: level, reset and elite reset. It's all HTML code but with a lot of escaping because the LVL RR ERR format is <number>(<number>)[<number>], and the brackets must be escaped.
# I am
NOT sure if it's necessary to have such a long match, it might be possible to get the same readings with a smaller search criteria. I just wanted to be sure of what I get.
# what the matrix
$results1 contains after the code is executed:
# level, reset and elite reset are contained in
[1][0],
[2][0] respectively
[3][0].
Code:
preg_match_all("/<td bgcolor=black width=30><font face=Arial size=2 font color=white>([A-Za-z0-9]{1}|[A-Za-z0-9]{2}|[A-Za-z0-9]{3}|[A-Za-z0-9]{4}|[A-Za-z0-9]{5}|[A-Za-z0-9]{6}|[A-Za-z0-9]{7}|[A-Za-z0-9]{8})<\/b><\/font><\/td>/", $site, $results2);
# this will catch the status and the guild name. The syntax is slightly different: we search for only one string (we have only 1 pair of
( ) ) and it must be numbers, letters or numbers+letters. if we don't use all that criteria, we will end up with the result from LVL, RR, ERR (brackets included) in here too and we don't need that. so what is between
( ) brackets says: search for any "something" that is made of 1, 2, 3, 4, 5, 6, 7 or 8 numbers / letters / numbers&letters.
# syntax:
[A-Za-z0-9] = any letter from A to Z that is upper-case + any letter from a to z that is lower-case + any number from 0-9;
{4} = that occurs 4 times (e.g. this will detect a string like "n00b");
| = or;
( ) = marks the search criteria;
# content of
$results2 after execution:
Now we finished with reading the site, we collected all the required data. Let's list the variables that hold the relevant values:
- $results0[1][0] = On-line status
- $results1[1][0] = Level
- $results1[2][0] = Reset
- $results1[3][0] = Elite reset
- $results2[1][0] = Strength
- $results2[1][1] = Agility
- $results2[1][2] = Vitality
- $results2[1][3] = Energy
- $results2[1][4] = Command
- $results2[1][5] = Guild name
This values will be written later on the signature we uploaded.
Code:
$textcolor = imagecolorallocate($im, 48, 255, 0);
$textcolorRED = imagecolorallocate($im, 255, 0, 0);
# set the colour of the font.
# the 3 numbers after $im are the RGB values for the colors I chose (a shade of green complementary to pink, and red for second). They have the same value that any other program will show (e.g. here, Photoshop):
Code:
$font = imageloadfont("gd_fonts/font.gdf");
# loading the font that we will use.
Code:
imagestring($im , $font , 38 , 5, $results1[1][0] , $textcolor );
# beginning the actual writing on the signature. Here, we wrote the level.
# syntax:
(<image we write on (here,
$im)>
, <font we will use; if you don't use another font, here you will write the size of the font (1 to 5)>
, X coordinate
, Y coordinate
, <what to write; here, we just dump the variable's value>
, <colour of font>
)
# the
X and
Y coordinates will say where to start writing from. They are equal to pixels. PHP calculates X & Y starting from top left corner of the image.
You should change them until they get in the desired position.
Code:
if ( $results1[2][0] == NULL ) {
imagestring ( $im , $font , 106 , 6 , "0" , $textcolorRED );
}
else {
imagestring( $im , $font , 106 , 6 , $results1[2][0] , $textcolor );
}
# writing the reset; we use an if() because if the character has no resets the $results1[2][0] doesn't get displayed (it should but it doesn't; a bug I haven't tracked down yet)
# if the character is 0rr we display 0 in red color
Code:
imagestring ( $im , $font , 41 , 25, $results2[1][0] , $textcolor );
imagestring ( $im , $font , 41 , 41, $results2[1][1] , $textcolor );
imagestring ( $im , $font , 41 , 56, $results2[1][2] , $textcolor );
imagestring ( $im , $font , 41 , 71, $results2[1][3] , $textcolor );
# writing the strength, agility, vitality and energy.
# you notice that we don't write elite reset and command, but they can be written if needed. For Command you just need to insert one more similar line of code and ajust it where to be written. For elite reset you can add it too but it's just a switch (0 or 1) so you would rather do a signature that writes already you are elite and just display the usual resets. In this case elite rr and command aren't needed on the signature.
Code:
if ($results2[1][5]==NULL){
imagestring($im , $font , 57 , 93, "NO GUILD" , $textcolorRED );
}
else{
imagestring($im , $font , 57 , 93, $results2[1][5] , $textcolor );
}
#we use an "
if" statement because, if the character is in no guild we want the signature to say so, in red. If the character is in a guild, then we write the guild's name, in normal color.
Code:
if ($results0[1][0]=="Online"){
imagestring($im, $font, 183, 93, "YES", $textcolor);
}
else{
imagestring($im, $font, 183, 93, "NO", $textcolor);
}
# similar for the online status. If character is online we write "
YES" using the normal text color, otherwise "
NO" using red text color.
Code:
header("Content-type: image/png");
imagepng($im, NULL, 0, NULL);
?>
# End of File
# because the output will be in a browser we set the header type and then output the image.
# syntax:
(<image to be output>
, <file to output to; here, we put
NULL because we don't want to save the file, we just want to show it directly>
, <png compression; here,
0 
aximum 9; remember png compression is lossless, it's just that I haven't tried the setting yet that I left it 0>
, <filters>
)
After you finished writing, save and upload "
generate.php" to "
./sig_example/sig.jpg/includes/".
Almost finished now, hang in there
.