[Tutorial] Render player data in a picture
#1

...Do what?
You know those images you see on some server's websites that display information about the player? Such as their score, their total time playing, etc. Well they're actually fairly easy to make, and with the new HTTP function it's easier then ever.

For an example on rendering an image, this picture displays your IP:


(refer to resources 'image2.php' at the bottom of the page for at look at the complete script)

What you will need
- You will need to get a font file (.tff). You can download them off the internet (dafont.com) or you can use one of your Windows defaults (assuming your using Windows) located in C:\Windows\Fonts.
- You will need an image to use for your background.
- You will need a webserver with PHP installed. Localhost will not work in this tutorial, you cannot send a HTTP request from the samp_server.exe to 127.0.0.1. For this tutorial I am using a free host at byethost.com.
- Some experience in PHP is also... recommended.

[php] Rendering process - Part 1
I assume you now have a folder with your .tff font and background image in it. I assume you also have a webserver that allows/has PHP installed.

Just rendering the very basic image is fairly straight forward.

PHP код:
<?php
    header
('Content-type: image/png'); //1
    
$image imagecreatefrompng('picture.png'); //2
    
imagepng($image); //3
    
imagedestroy($image); //4
?>
1) We are telling PHP to display this .php file as a .png image. (header)
2) We are assigning the variable $image to our background, which in my case is picture.png. (imagecreatefrompng)
3) We're telling PHP to display the image. (imagepng)
4) We're getting rid of the image after it has been displayed. (imagedestroy)

This will simply display this: http://zezombia.byethost22.com/image1.php

(refer to resources 'image1.php' at the bottom of the page for at look at the complete script)

[PAWN] Sending information from samp_server.exe to the webserver
Now, I'm sure all of you have your different opinions on how to store your player's data files. I'm using a very simplistic method so people can understand how it works, not necessarily the best way of storing information.

This is the wiki page on the HTTP function. The only things we are going to focus on is type, and url. The rest can stuff it for now. We are going to send the data through the url itself.

For type, we are going to use HTTP_HEAD because we aren't going to bother with a response code. For url, this is the url to what will soon be the opposite end of this section. This pawn script will send the data, this php file will save the information to the server.

In php, we are going to be using the $_GET feature. Basically, we're going to make the script grab the end of the url - and this will contain the data such as the players score and his hours played. For me, the php script that will be on the receiving end is located at http://zezombia.byethost22.com/get.php. In this case, we are also going to be sending the players username over the url.

http://zezombia.byethost22.com/get.php?user=Zezombia&score=42&hours=2

There's a question mark to start the $_GET data we're sending, and we use 'and' (&) symbols to separate multiple variables we use. Just by visiting that url, we can set the user's score, their hours, all that good stuff.

pawn Код:
HTTP(-1, HTTP_HEAD, "zezombia.byethost22.com/get.php?user=Zezombia&score=42&hours=2", "", "");
I used -1 as the player ID because this doesn't relate to anyone, and the Data and Callback features are just left blank. Of course this will be used in whatever situation you're changing your player's score or time playing etc. Remember, you don't need to send more then 1 piece of data at once, but in my case we need to include the username anyway.

(refer to resources 'image.pwn' at the bottom of the page for at look at the complete script)

[php] Receiving information from samp_server.exe
We are going to create our get.php file. Very very first of, we want to make sure that information can only be sent from the server's IP. If just anyone could change they're pictures data, it wouldn't cause much harm but it would be cheap.

PHP код:
if($_SERVER['REMOTE_ADDR'] != '142.167.164.171') return false
That checks if your IP matches (in my case) 142.167.164.171. If it does not, it returns false, and the script stops processing. Next we are going to be using the isset function. This feature checks if a variable exists.

PHP код:
if(isset($_GET['user']) == false) return false
That checks if the user=Zezombia has been included in the url (if the variable actually exists). If it does not, then discontinue the script from loading (return false). What we're going to do next is check every possible variable that could have been sent, and if they do exist then we're going to modify the data file which contains the players information. Note again that I'm using a very simplistic method, and every variable will be saved in their own individual file.

PHP код:
if(isset($_GET['score']) == true//1
{
    
$file fopen('users/' $_GET['user'] . '.score''w'); //2
    
fwrite($file$_GET['score']); //3
    
fclose($file); //4

1) Checks if the variable is actually set/exists. (isset)
2) We're setting the variable $file to handle what file we're opening. It opens the file Zezombia.score. This file is contained in the /users/ directory. The 'w' means we are going to write to the file. (fopen)
3) We now write the score to the ($)file. (fwrite)
4) We close the file. (fclose)

We would do that process for every variable we could send. Score, health, hours played, people killed, times dies, basically anything you want the picture to display.

(refer to resources 'get.php' at the bottom of the page for at look at the complete script)

[PAWN/OPTIONAL] Making sending information more simple
I didn't do this, but I just thought of this now. Wouldn't you prefer to just use a function like:

pawn Код:
SetPlayerWebserverScore(playerid, score);
Then messing with the whole $_GET functionality? Well all you would have to do is make a function such as:

pawn Код:
SetPlayerWebserverScore(playerid, score)
{
    //get the player's name, and create a string variable
    format(string, sizeof(string), "zezombia.byethost22.com/get.php?user=%s&score=%d", pname, score);
    HTTP(-1, HTTP_HEAD, string, "", "");
}
I'm not going to go too far into this but it's a easy little tip.

[php] Rendering process - Part 2
The last thing we have to do is finish our php file which displays the image. So first off, we want to obtain the information about the player that we are going to display.

PHP код:
$file fopen('users/' $_GET['user'] . '.score''r'); //1
$score fread($filefilesize('users/' $_GET['user'] . '.score')); //2
fclose($file); //3 
1) Open the same file that we did in the last part, but this time we are going to read it ('r'). (fopen)
2) We are going to set the $score variable to the number that Zezombia.score contains. We also have to include the size of the file when reading from a file. (fread)
3) We close the file. (fclose)

Now we want to define the color of the text we are going to be using. This simply uses the RGB method. For this tutorial we are going to be using black, which RGB is 0, 0, 0. (imagecolorallocate)

PHP код:
$black imagecolorallocate($image000); 
So now that we've got our color set, all we have to do is print text onto the image. We will be using the php imagettftext function. The syntax for this function is:
imagettftext($image, size, Float:angle , x, y, color, fontfile[], text[])

The image variable we used in this tutorial is $image. The font size we're going to use is 15. We're not going to have a rotation/angle (0). We're going to be print 3 different pieces of text onto our image, but the first one's x and y are going to be 15 and 75. $black is going to be our color, the font file we're using in this tutorial is BAUHS93.TTF, and for the first line of text we're going to be printing the player's name.

We're going to print the player's name as it is displayed on the url. What I mean is, to determine which person the image belongs to, we're going to include the name of the person on the url and use the $_GET function to achieve and display it. So the url to our image will look like: http://zezombia.byethost22.com/image3.php?user=Zezombia

PHP код:
imagettftext($image1501545$black'BAUHS93.TTF'$_GET['user']); 
When we are done everything, the image we used in this tutorial will display as:


(refer to resources 'image3.php' at the bottom of the page for at look at the complete script)

I'm stealing [FU]Victious's signature as another example as to what can be done with this. It was made by Andre9977:


Resources
These are the full scripts I used for the examples on this tutorial.

image2.php
image1.php
image.pwn
get.php
image3.php

Download the source for the site used in this tutorial here: http://zezombia.byethost22.com/source.rar. The link isn't broken but byethost is dumb so you may need to copy the url into your address bar.

This may not be the most efficient way of doing this, but it's the easiest from what I've seen so far.
Reply
#2

nice job
Reply
#3

Yaay, thanks for the tutorial it will help some other people too
Reply
#4

WOW! Thanks alot for this.
Reply
#5

LMAO O_O before i was actually gonna read, i saw my ip in that pic. and i thought: wtf, why are u using my ip in your tutorial xDD
haha,

nice tut
Reply
#6

Thank u man, I was searching it 2 months.
Reply
#7

haha, worked but
where did u get the example pic?
my server ip is 83.85.39.29:7777 -.-
and the pic has same ip
Reply
#8

Quote:
Originally Posted by Mike_Peterson
Посмотреть сообщение
haha, worked but
where did u get the example pic?
my server ip is 83.85.39.29:7777 -.-
and the pic has same ip
The pic shows YOUR IP, I was like WTF is this shit as well when I saw my own IP hehe.

This tut might become very useful for me in the near future, thanks
Reply
#9

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
The pic shows YOUR IP, I was like WTF is this shit as well when I saw my own IP hehe.

This tut might become very useful for me in the near future, thanks
haha, i thought the same :P

dutch wait error:
Dit forum vereist dat je 120 seconden wacht tussen het verzenden van berichten. Probeer het nogmaals over 70 seconden.

28
19
14
OMGGG
3
finally
Reply
#10

for mysql would be also good..
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)