by oorf I f*ckfish aka Alexander P.
http://fish.stabb.de
This class allows you to easily output Trackmania Forever styled strings to HTML and images just like signature pics for example.
Download
TMF Color Parser All Versions
Download includes fully working usage examples
Update v1.3c:
- forgot a php closing tag in the class - fixed (thx Sascha)
- fixed a link parsing problem which occured when there were ip adresses as links, thanks to w1lla
- the image rendering always drew a shadow below the text, this can now be turned off so that a shadow is only drawn if there is actually a shadow tag in the input string
- Manialinks did not have a HTML closing tag, fixed (thanks to yorkshire)
- Added a feature to strip every formatting tag you want from the input string, this could be a single tag or a list of tags (see 'Documentation' section below)
- Added a color correction to automatically change colors so that they are readable on a given background (see 'Documentation'->'Color Correction' section below)
- Now correctly displays $l-tags that only use "www." instead of "http://www."
- The parser now transforms Manialinks into tmtp-calls that are opened inside the gameclient (in version 1.0 they led to smurfs manialink viewer)
You can use this class to display Trackmania styled strings on your homepage. You just input for example a nickname like '$f00f*ck$f80fish' and it will return a nearly exact version of this nickname (that would look similar to this: f*ckfish) in valid HTML/CSS or paint it on an image.
This class bases on the old TMN Color Parser but extends the functionality to Forever specific tags. At the moment the Color Parser understands all tags including the link and manialink variations resulting in actual links inside the HTML output. The manialink tags link to smurfs Manialink Viewer (I hope it will be fully working with Forever in the near future).
Unfortunately not all TM tags can be displayed correctly with the help of HTML and CSS yet.
Example
This example shows the simplicity of the usage of the class:
Code: Select all
<?
//include class
require_once('./class/tmfcolorparser.inc.php');
$cp= new TMFColorParser();
//output a formatted nickname first as HTML then as an image
$nickname = '$i$f00f*ck$f80fish';
echo $cp->toHTML($nickname)."<br>";
echo "<img src='image.php?nick="
.urlencode($nickname)."' border = 0/><br>";
?>
See the TMFColorParser in action on http://fish.stabb.de/tmfcolorparser.php5
Documentation
To use the ColorParser you will first have to include the class into your php script like that:
Code: Select all
require_once('./classes/tmfcolorparser.inc.php');
Instantiation
To instantiate the ColorParser object, you'll need to call the constructor:
Code: Select all
function TMFColorParser($autoContrastToBackgroundColor = '')
So those would be valid ways to instantiate a ColorParser object:
Code: Select all
$cp = new TMFColorParser();
$cp2 = new TMFColorParser('#0088ff');
If you want to use the ColorParser to convert TrackMania styled strings to HTML you'll need to use the toHTML function:
Code: Select all
function toHTML($str, $stripColors = false, $stripLinks = false, $stripTags = '')
- $str - The TrackMania styled string (e.g. '$08ffoo$wbar')
- $stripColors - (optional) - if set to true the ColorParser will remove all colors from the string
- $stripLinks - (optional) - if set to true the ColorParser will remove all links from the string (Weblinks and Manialinks)
- $stripTags - (optional) - this one is used to strip all remaining formatting tags from the string, this parameter takes a string that consists of all the tags you want the ColorParser to get rid of. Examples:
- 'i' - removes all italic tags from the string
- 'wo' - removes all bold/wide tags (according to '$w' and '$o')
- 'ni' - removes all narrow and italic formatting (according to '$n' and '$i')
- you can use all combinations you want to, there's one special reserved string, though:
- 'all' - removes all known formatting tags, so the ColorParser only outputs plain unformatted text
Code: Select all
$nick='$08f$if*ck$w$f00fish';
echo $cp->toHTML($nick);
echo $cp->toHTML($nick, true); //without colors
echo $cp->toHTML($nick, false, true); //with colors, without links
echo $cp->toHTML($nick, false, false, 'i'); //with colors, with links, without italics
To be able to use this feature you will have to use the php GD library. For some basics on how to create and output an image, please refer to the included "image.php". I'll just focus on the following function that draws TM Strings into an existing GD image:
Code: Select all
function drawStyledString($src_img,$size, $x, $y , $color, $font, $text, $stripColors = false, $stripTags = '')
- $src_img - The handle to the image you want to draw the string on
- $size - The font size
- $x, $y - The pixel position where to draw the string
- $color - The color for letters that are not otherwise color coded (default color)
- $font - The path to the font that will be used (without file extension). You will need 4 different font files that have to follow specific naming conventions (a variant of Tahoma is included):
- tahoma.ttf - normal style
- tahomait.ttf - italic style
- tahomawd.ttf - wide style
- tahomawdit.ttf - wide and italic style
- $text - The TrackMania styled string
- stripColors - (optional) - if set to true the ColorParser will remove all colors from the string
- stripTags - (optional) - for usage, see the explanation right above in the "HTML Output" section
Code: Select all
// before this you'll have to create an image handle, and a color, see "image.php" for an example
$text='$08f$if*ck$w$f00fish';
$cp->drawStyledString($imgHandle, 9, 10, 10, $black, 'fonts/tahoma', $text); //default output
$cp->drawStyledString($imgHandle, 9, 10, 20, $black, 'fonts/tahoma', $text, true); //strip colors
$cp->drawStyledString($imgHandle, 9, 10, 30, $black, 'fonts/tahoma', $text, false, 'i'); //with colors, strip italics
Code: Select all
$cp->alwaysDrawFontShadows = false; //or true, default value is "true"
Since version 1.3 I implemented a color correction which alters the colors of the styled strings so that they are readable on a given background color. To achieve that, the ColorParser computes a difference value between the background color and the color it wants to display and brightens or darkens the color until a minimum difference is reached. This feature is optional. The color correction is activated by assigning a background color to the ColorParser, these can be done when you instantiate the ColorParser object (see 'Instantiation' section above) or by calling the following function anytime in your script before you output a string:
Code: Select all
function autoContrastColor($bgColor)
Valid usage example:
Code: Select all
$cp->autoContrastColor('#ff9900');
echo $cp->toHTML($text); //outputs a text that will be readable on a site with the background color #ff9900
$cp->autoContrastColor('');
echo $cp->toHTML($text); //outputs a text without color correction
$cp->autoContrastColor('#000000');
echo $cp->toHTML($text); //outputs a text that will be readable on a site with a black background
So if you use the default color correction, you will always be sure to be (mathematically) closer to the actually used color. Nevertheless some strings will still look weird so I built in some switches so you can force the ColorParser to be a bit more consistent in its color choosing process:
Code: Select all
$cp->forceBrighterColors();
Code: Select all
$cp->forceBrighterColors();
All color correction features work for both HTML and image rendering.
Screenshot
Below you can see the output of the included sample PHP file in my local browser.
