Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: How to make live stats images for your scripts

  1. #1
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default How to make live stats images for your scripts

    Original thread:

    http://villavu.com/forum/showthread.php?t=83538

    Recently a few members of Villavu have been trying to re-create signature which update to their server. Some have been having issues.

    Myself, Kyle and [J]ustin have done this to test it and found a few twists and turns in the tutorials which can throw people off, so this is hopefully a full fix for that tutorial.

    What will you learn?

    You will be learning a little PHP (Enough to progress you into further projects I think). Pascal and some logical thinking


    What will I need?


    • A Webhost with MYSQL Databasing
    • An Image


    Ok, so first things first we need to create a website, Putonajonny suggested:


    Steps from here:

    • Sign up using the registration, You don't want to pay so use a subdomain
    • Confirm your email
    • Go to the control panel of your selected sub domain, scroll or press CTRL + F to find Software / Services and click on the MYSQL icon (Should be a dolphin or something)
    • Create a new database and a user, SAVE THESE DETAILS. YOU WILL NEED THEM.
    • Now that you have a database and user, you need to manage it. So click on PHPMyAdmin and enter your database


    So from this point we are inside the Database, this can get a little confusing so we will take baby steps!

    The best way I can explain these databases is to imagine them like Excel spread sheets. The database is the document, the tables are the worksheets and all the parts inside is the data!

    Firstly we will need to create a new database. If you are using 000webhost you will have a different PHPMyAdmin to most main hosts but have no fear! It's pretty much the same.

    So following Putona's tutorial we will name the Table to be "Table1". (Case sensitive).
    From here we need to create fields! So for now add 3-4 depending on how many variables you are using.
    Here is an example of how the table details should look (I used Excel to explain it).



    Putona used this explanation which is fairly good describing all of the options:



    Your fields should now look like this:



    So far so good? I hope so!

    With my technique it doesn't matter what you use My Scripts name as, it's only for personal use so you can read it easier.

    Eventually with more scripts it will do this:



    What ID auto increment means is that each time you submit a new script name it will automatically add a new ID

    Connecting our database up.

    Ok, so now we need to create a PHP file!
    To do this, create a new file (Preferably a text file) and name it

    Commit.php (or anything you want.php) but make sure to save it as a PHP file and not a text file

    Inside here you want to put your Database information!

    PHP Code:
    <?PHP

        $user_name 
    "usename";  //Username for the account you created earlier
        
    $pass_word "password";  //Password for the account you created earlier
        
    $database "database";  //The name of the database you created earlier
        
    $server "MySQLServer";  //With 000webhost this can be found by going to your control panel then clicking MySQL
    We also need to actually connect the database...
    PHP Code:
        $db_handle mysql_connect($server$user_name$pass_word); or die(mysql_error());
        
    $db_found mysql_select_db($database$db_handle); or die(mysql_error()); 
    What this does is check the database is connecting and if not it will tell you the error

    Now we are going to check if the database is found and if it is then do the following!

    PHP Code:
    if ($db_found) {    
            
    $SQL "SELECT * FROM `Table1` WHERE `ID` = 1"
            
    $result mysql_query($SQL);
            if(
    $result){                        
                while (
    $db_field mysql_fetch_assoc($result)){
     
                    
    $time $db_field['Time'];
            
    $tokens $db_field['XP'];
                } 
    Now there are two parts in this, the table and ID + the Variables.

    $SQL = "SELECT * FROM `Table1` WHERE `ID` =1"; basically checks in Table1 and finds all of the information inside the row of ID1.

    PHP Code:
                    $time $db_field['Time'];
            
    $tokens $db_field['XP']; 
    Another image which will hopefully explain it!


    PHP Code:
    header("Content-type: image/png");
                
    putenv("GDFONTPATH=".realpath("."));   
     
                
    $img imagecreatefrompng("signature.png"); 
     
                
    $color["yellow"] = imagecolorallocate$img255255); 
                
    $color["white"] = imagecolorallocate$img255255255 ); 
                
    $color["green"] = imagecolorallocate$img0255); 
                
    $color["orange"] = imagecolorallocate$img254179); 
                
    $color["purple"] = imagecolorallocate$img1280128 ); 
                
    $color["dblue"] = imagecolorallocate$img3784199 ); 
    if you want to make it something other than a PNG, read this:
    http://php.net/manual/en/function.header.php

    $img = is the location of the background on your root.
    putenv("GDFONTPATH=".realpath("."));
    basically means the font should be in the root of the site


    This section is a little more creative!

    Now to change the text on the signature!
    PHP Code:
                                      //size     x  y
                
    imagettftextshadow($img2002530$color["dblue"], "font.ttf""Script name"); 
                
    imagettftextshadow($img1201056$color["orange"], "font.ttf""XP Gained: "); 
                
    imagettftextshadow($img1209056$color["purple"], "font.ttf"number_format($XP)); 
                
    imagettftextshadow($img1201076$color["orange"], "font.ttf""Total Time: "); 
                
    imagettftextshadow($img12012076$color["purple"], "font.ttf"formatMilliseconds($time 1000)); 
     
                
    imagepng($img); 
                
    imagedestroy($img);
            } 
    This is pretty much it, you have defined your color, the text and what its printing..

    E.g the $ stuff

    SIMBA part:

    Add this in the mainloop:

    AddOnTerminate('SendData');//To make it send the data

    Simba Code:
    Function SendData : Boolean;
    Var
      i : integer;
    begin

      i := InitializeHTTPClientWrap(False);  //this creates a client

      AddPostVariable(i, 'time', IntToStr(GetTimeRunning / 1000));//This adds our info to the client
      AddPostVariable(i, 'runs', IntToStr(XPGained));

      Result := Pos('true', Lowercase(PostHTTPPageEx(i, 'http://mywebsite.net/MyPage.php'))) <> 0;//Then this posts it

      FreeHTTPClient(i);//Remember to free it

    end;


    Full PHP bit:

    PHP Code:

    <?PHP   //Milliseconds Function

        
    function formatMilliseconds($milliseconds) {
                
    $seconds floor($milliseconds 1000);
                
    $minutes floor($seconds 60);
            
    $hours floor($minutes 60);
            
    $days floor($hours 24);
            
    $milliseconds $milliseconds 1000;
                
    $seconds $seconds 60;
            
    $minutes $minutes 60;
            
    $hours $hours 24;

                if(
    $days == 0){
                
    $format '%uhours, %02uminutes and %02u seconds';
                    
    $time sprintf($format$hours$minutes$seconds);
                    return 
    rtrim($time'0');
            }else{
                
    $format 'Úys, %uhours, %02uminutes';
                    
    $time sprintf($format$days$hours$minutes$seconds);
                    return 
    rtrim($time'0');
            }
                
        }
        
    //imagettftext function
        
    function imagettftextshadow($image$size$angle$x$y$color$fontfile$text) { 
              
    imagettftext($image$size$angle$x 1$y 1imagecolorallocate($image000), $fontfile$text); 
              
    imagettftext($image$size$angle$x$y$color$fontfile$text); 
        }
    //Database

        
    $user_name "DBUsername";  //Username for the account you created earlier 
        
    $pass_word "Password";  //Password for the account you created earlier 
        
    $database "DBname";  //The name of the database you created earlier 
        
    $server "localhost";  //With 000webhost this can be found by going to your control panel then clicking MySQL     

        
    $db_handle mysql_connect($server$user_name$pass_word) or die(mysql_error());
        
    $db_found mysql_select_db($database$db_handle) or die(mysql_error());
        
         
    //Script
        
    if ($db_found) {    

            
    $SQL "SELECT * FROM `table1` WHERE `ID` = 1";   
            
    $result mysql_query($SQL);
            if(
    $result){                        
                while (
    $db_field mysql_fetch_assoc($result)){

                    
    $time $db_field['Time'];
                    
    $xp $db_field['XP'];
                }
                
                
    header("Content-type: image/png");
                
    putenv("GDFONTPATH=".realpath("."));   

                
    $img imagecreatefrompng("sig.png"); 

                
    $color["yellow"] = imagecolorallocate$img255255); 
                
    $color["white"] = imagecolorallocate$img255255255 ); 
                
    $color["green"] = imagecolorallocate$img0255); 
                
    $color["orange"] = imagecolorallocate$img254179); 
                
    $color["purple"] = imagecolorallocate$img1280128 ); 
                
    $color["dblue"] = imagecolorallocate$img3784199 ); 

                
    imagettftextshadow($img1201050$color["orange"], "sans.ttf""XP Gained: "); 
                
    imagettftextshadow($img1209050$color["purple"], "sans.ttf"number_format($xp)); 
                
    imagettftextshadow($img1201070$color["white"], "sans.ttf""Time Run: "); 
                
    imagettftextshadow($img1209070$color["purple"], "sans.ttf"formatMilliseconds($time 1000)); 

                
    imagepng($img); 
                
    imagedestroy($img);
            }else
                        print 
    "Not working";    
            
        }
        else 
            print 
    "DB not Found, please report this";
            

    ?>
    SQL Injection:


    [J]ustin reminded me of this ^^

    SQL Injection can be a pain in the arse, they can find your details, put stuff onto your website etc. So you need protection!
    This is the function:
    PHP Code:
    mysql_real_escape_string(); 
    This is it in use:
    PHP Code:
    mysql_real_escape_string($SQL); 
    Tutorial:

    http://www.tizag.com/mysqlTutorial/m...-injection.php
    Last edited by xtrapsp; 01-02-2013 at 10:04 AM.

  2. #2
    Join Date
    Jan 2012
    Location
    Calgary, AB, Canada
    Posts
    1,819
    Mentioned
    5 Post(s)
    Quoted
    120 Post(s)

    Default

    nice tut man
    Current Project: Retired

  3. #3
    Join Date
    Jun 2012
    Posts
    4,867
    Mentioned
    74 Post(s)
    Quoted
    1663 Post(s)

    Default

    I may want to try this, could it work for avatars too?

  4. #4
    Join Date
    Mar 2007
    Posts
    5,125
    Mentioned
    275 Post(s)
    Quoted
    901 Post(s)

    Default

    Wont work for avatars.

    You might want to add some sql injection protection there Brad

    Forum account issues? Please send me a PM

  5. #5
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by BMWxi View Post
    I may want to try this, could it work for avatars too?
    I don't believe so. It's worth a try

  6. #6
    Join Date
    Mar 2012
    Location
    Canada
    Posts
    870
    Mentioned
    1 Post(s)
    Quoted
    5 Post(s)

    Default

    Disregard this post.

    I didn't think before posting -.-
    My scripts:
    Advanced Barb Agility Course(outdated), MonkeyThieverV0.11, MahoganyTableV0.4(outdated)
    Questions? I bet that for 98% of those, you'll find answer HERE

  7. #7
    Join Date
    Mar 2007
    Posts
    5,125
    Mentioned
    275 Post(s)
    Quoted
    901 Post(s)

    Default

    Quote Originally Posted by xtrapsp View Post
    I don't believe so. It's worth a try
    Don't forget to add that you will need to edit the script to calculate the 'XPGained' variable

    Forum account issues? Please send me a PM

  8. #8
    Join Date
    Jul 2011
    Location
    /home/litoris
    Posts
    2,226
    Mentioned
    0 Post(s)
    Quoted
    159 Post(s)

    Default

    Nice tut, too bad stats has been down since March
    Miner & Urn Crafter & 07 Chicken Killer
    SPS BlindWalk Tutorial

    Working on: Nothing

    teacher in every art, brought the fire that hath proved to mortals a means to mighty ends

  9. #9
    Join Date
    Mar 2007
    Posts
    5,125
    Mentioned
    275 Post(s)
    Quoted
    901 Post(s)

    Default

    Quote Originally Posted by litoris View Post
    Nice tut, too bad stats has been down since March
    This doesn't use SRL Stats.

    Forum account issues? Please send me a PM

  10. #10
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by litoris View Post
    Nice tut, too bad stats has been down since March
    It doesn't use SRL stats, it uses your own ^^

  11. #11
    Join Date
    Mar 2007
    Posts
    5,125
    Mentioned
    275 Post(s)
    Quoted
    901 Post(s)

    Default

    Quote Originally Posted by xtrapsp View Post
    It doesn't use SRL stats, it uses your own ^^
    Feel the power of being ninja'd

    Forum account issues? Please send me a PM

  12. #12
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default

    There is a mistype at the part "Full PHP bit:".
    PHP Code:
    $format 'Úys, %uhours, %02uminutes'

  13. #13
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    1,631
    Mentioned
    47 Post(s)
    Quoted
    254 Post(s)

    Default

    Might want to add error reporting
    PHP Code:
    error_reporting(-1); 
    Got everything working now thanks to Shatterhands tutorial and help on Skype. Hope this will work for anyone without help.

    Script source code available here: Github

  14. #14
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by Shatterhand View Post
    There is a mistype at the part "Full PHP bit:".
    PHP Code:
    $format 'Úys, %uhours, %02uminutes'
    I think thats a format issue with the forum, it keeps changing it. I think the PHP tag isn't utf-8 or something ^^

  15. #15
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    1,631
    Mentioned
    47 Post(s)
    Quoted
    254 Post(s)

    Default

    Can you add information about adding caching to your images? Because my database is offline now because I put the image in my signature

    I think something like this (http://dtbaker.com.au/random-bits/ho...ed-by-php.html) will work? I'm not sure about PHP-related stuff and I don't want to overflow the site again. Thanks in advance

    Script source code available here: Github

  16. #16
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by J J View Post
    Can you add information about adding caching to your images? Because my database is offline now because I put the image in my signature

    I think something like this (http://dtbaker.com.au/random-bits/ho...ed-by-php.html) will work? I'm not sure about PHP-related stuff and I don't want to overflow the site again. Thanks in advance
    Didnt know about caching, thanks for the tip!
    EDIT: Its not working nvm.
    EDIT2: Googled a lot, here is what I found:
    source ->
    PHP Code:
    function getRequestHeaders() 

        if (
    function_exists("apache_request_headers")) 
        { 
            if(
    $headers apache_request_headers()) 
            { 
                return 
    $headers
            } 
        } 
        
    $headers = array(); 
        if (isset(
    $_SERVER['HTTP_IF_MODIFIED_SINCE'])) 
        { 
            
    $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE']; 
        } 
        return 
    $headers
    }

    // Return the requested graphic file to the browser 
    // or a 304 code to use the cached browser copy 
    function displayGraphicFile ($graphicFileName$fileType='jpeg'

        
    $fileModTime filemtime($graphicFileName); 
        
    // Getting headers sent by the client. 
        
    $headers getRequestHeaders(); 
        
    // Checking if the client is validating his cache and if it is current. 
        
    if (isset($headers['If-Modified-Since']) && 
            (
    strtotime($headers['If-Modified-Since']) == $fileModTime)) 
        { 
            
    // Client's cache IS current, so we just respond '304 Not Modified'. 
            
    header('Last-Modified: '.gmdate('D, d M Y H:i:s'$fileModTime).
                    
    ' GMT'true304); 
        } 
        else 
        { 
            
    // Image not cached or cache outdated, we respond '200 OK' and output the image. 
            
    header('Last-Modified: '.gmdate('D, d M Y H:i:s'$fileModTime).
                    
    ' GMT'true200); 
            
    header('Content-type: image/'.$fileType); 
            
    header('Content-transfer-encoding: binary'); 
            
    //header('Content-length: '.filesize($graphicFileName)); 
            //readfile($graphicFileName); 
        


    displayGraphicFile("image.png"); 
    These functions will change the caching setting of the header.
    Remove the line "header("Content-type: image/png"); " and put the code before it.
    It works.
    EDIT3: Just realized that texts dont get updated on the cached picture.
    Last edited by Shatterhand; 01-15-2013 at 11:18 PM.

  17. #17
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    Kyles your man for this ^

  18. #18
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by Shatterhand View Post
    Didnt know about caching, thanks for the tip!
    EDIT: Its not working nvm.
    EDIT2: Googled a lot, here is what I found:
    source ->
    PHP Code:
    function getRequestHeaders() 

        if (
    function_exists("apache_request_headers")) 
        { 
            if(
    $headers apache_request_headers()) 
            { 
                return 
    $headers
            } 
        } 
        
    $headers = array(); 
        if (isset(
    $_SERVER['HTTP_IF_MODIFIED_SINCE'])) 
        { 
            
    $headers['If-Modified-Since'] = $_SERVER['HTTP_IF_MODIFIED_SINCE']; 
        } 
        return 
    $headers
    }

    // Return the requested graphic file to the browser 
    // or a 304 code to use the cached browser copy 
    function displayGraphicFile ($graphicFileName$fileType='jpeg'

        
    $fileModTime filemtime($graphicFileName); 
        
    // Getting headers sent by the client. 
        
    $headers getRequestHeaders(); 
        
    // Checking if the client is validating his cache and if it is current. 
        
    if (isset($headers['If-Modified-Since']) && 
            (
    strtotime($headers['If-Modified-Since']) == $fileModTime)) 
        { 
            
    // Client's cache IS current, so we just respond '304 Not Modified'. 
            
    header('Last-Modified: '.gmdate('D, d M Y H:i:s'$fileModTime).
                    
    ' GMT'true304); 
        } 
        else 
        { 
            
    // Image not cached or cache outdated, we respond '200 OK' and output the image. 
            
    header('Last-Modified: '.gmdate('D, d M Y H:i:s'$fileModTime).
                    
    ' GMT'true200); 
            
    header('Content-type: image/'.$fileType); 
            
    header('Content-transfer-encoding: binary'); 
            
    //header('Content-length: '.filesize($graphicFileName)); 
            //readfile($graphicFileName); 
        


    displayGraphicFile("image.png"); 
    These functions will change the caching setting of the header.
    Remove the line "header("Content-type: image/png"); " and put the code before it.
    It works.
    EDIT3: Just realized that texts dont get updated on the cached picture.
    Try refreshing with CTRL + F5?

  19. #19
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by xtrapsp View Post
    Try refreshing with CTRL + F5?
    Ye it works, but a normal user would not do CTRL + F5. This is not the best solution.

  20. #20
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by Shatterhand View Post
    Ye it works, but a normal user would not do CTRL + F5. This is not the best solution.
    It simply means your not having your cache cleared regularly, I'l look into it when I have time ^^

  21. #21
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by xtrapsp View Post
    It simply means your not having your cache cleared regularly, I'l look into it when I have time ^^
    Im not sure what you mean.
    JJ and me wants our images (lot of Kb) not to load every time we load the page.
    Normal images get cached by browser automatically, but .php images dont.
    So we want our .php images to have cached, but ONLY the image, the text on should be up-to-date.

    Here is an example:
    not cached image, up-to-date text: ->
    cahced image, not up-to-date text: ->

  22. #22
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by Shatterhand View Post
    Im not sure what you mean.
    JJ and me wants our images (lot of Kb) not to load every time we load the page.
    Normal images get cached by browser automatically, but .php images dont.
    So we want our .php images to have cached, but ONLY the image, the text on should be up-to-date.

    Here is an example:
    not cached image, up-to-date text: ->
    cahced image, not up-to-date text: ->
    Hmm, the only thing that I question with that is that it's saving the text on the image isn't it? so surely it's a matter of detecting if the time is different from when u last cached it, deleting and reuploading?

    @ Kyle

  23. #23
    Join Date
    Jan 2012
    Posts
    1,104
    Mentioned
    18 Post(s)
    Quoted
    211 Post(s)

    Default

    Quote Originally Posted by xtrapsp View Post
    Hmm, the only thing that I question with that is that it's saving the text on the image isn't it? so surely it's a matter of detecting if the time is different from when u last cached it, deleting and reuploading?
    @ Kyle
    What if the text changes every minute or so? It would force to load the image every time you load the page. We should separate the image and text somehow, but not sure how. This caching applies to the whole header.

  24. #24
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    Quote Originally Posted by Shatterhand View Post
    What if the text changes every minute or so? It would force to load the image every time you load the page. We should separate the image and text somehow, but not sure how. This caching applies to the whole header.
    If we seperated the image to the text it wouldn't be an image format then surely?

  25. #25
    Join Date
    Oct 2008
    Location
    C:\Simba\Includes\
    Posts
    7,566
    Mentioned
    19 Post(s)
    Quoted
    180 Post(s)

    Default

    You cannot "split" the text from the image. That's why you're printing out the text on top of the image, to make it a PHP image. It's either you cache everything, or you don't cache at all. Because even if the text changes, you'll have to create a whole new image, which defeats the purpose of caching.

    I'd do an hour cache, that's the normal for these type of things.
    Away for awhile, life is keeping me busy. | Want to get my attention in a thread? @Kyle Undefined; me.
    { MSI Phoenix || SRL Stats Sigs || Paste || Scripts || Quotes || Graphics }

    When posting a bug, please post debug! Help us, help you!

    I would love to change the world, but they won't give me the source code. || To be the best, you've got to beat the rest. || Logic never changes, just the syntax.
    If you PM me with a stupid question or one listed in FAQ, or about a script that is not mine, I will NOT respond.


    SRL is a Library of routines made by the SRL community written for the Program Simba. We produce Scripts for the game Runescape.


Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •