[PHP/HTML] TMF Color Parser 1.3c

This is the place where you can find everything related to the dedicated server, control scripts and community tools.

Moderators: Pit Crew, TM-Patrol

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

[PHP/HTML] TMF Color Parser 1.3c

Post by f*ckfish » 24 Apr 2008 12:12

TMF Color Parser v1.3c (for PHP)
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)
Update v1.3b:
  • fixed a link parsing problem which occured when there were ip adresses as links, thanks to w1lla
Update v1.3:
  • 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)
Update v1.2:
  • Now correctly displays $l-tags that only use "www." instead of "http://www."
Update v1.1:
  • The parser now transforms Manialinks into tmtp-calls that are opened inside the gameclient (in version 1.0 they led to smurfs manialink viewer)
Typical Usage

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>";
?>
More examples and variations to strip the links and/or the colors are included in the download.

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');
Now you are free to access all of its features. Let's begin by creating a ColorParser object.


Instantiation

To instantiate the ColorParser object, you'll need to call the constructor:

Code: Select all

function TMFColorParser($autoContrastToBackgroundColor = '')
The parameter $autoContrastToBackgroundColor is optional and needs a color as a hexadecimal string. If you leave it empty, the ColorParser will not use color correction, if you specify a color, the ColorParser will always try to maintain a contrast level between this color and the color of the outputted string to increase readability.

So those would be valid ways to instantiate a ColorParser object:

Code: Select all

$cp = new TMFColorParser();
$cp2 = new TMFColorParser('#0088ff');
HTML Output

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 = '')
The parameters are used like that:
  • $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
These would be some valid calls to the function:

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
Image Output

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 = '')
The parameters are used like that:
  • $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
Valid usage examples:

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
In older versions I always drew a shadow below the text to make it readable on any background. Since there is the color correction feature now, you are able to toggle the shadow by changing the following value before you draw the string:

Code: Select all

$cp->alwaysDrawFontShadows = false; //or true, default value is "true"
Color Correction

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)
$bgColor is a hexadecimal coded color string against which every color that is found in the string will be checked. If you call this function with an empty string as the parameter, the color correction will be turned off.
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
As you see in the sample screenshot below, there may be some oddities which cause some colors to look weird. This is because of the algorithm, which always brightens and darkens the color at the same time and as soon as both have the same difference from the background color it choses the color which has the less difference to the actual color.
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();
will force the color correction to always brighten the colors and

Code: Select all

$cp->forceBrighterColors();
will force it to always darken the colors.

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.

Image
inactive, sorry

User avatar
SmashingDeluXe
sunday driver
sunday driver
Posts: 58
Joined: 04 Dec 2007 20:38
Owned TM-games: All
Manialink(s): NO
Location: Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by SmashingDeluXe » 24 Apr 2008 13:19

:thumbsup:
Hey again nice Work fish!
I test it on my homepage and the response of the user and me is the same!

Awesome! Now it´s for TM User a lot easier to write some colors in forums ect.
But why not update the TM Forum with this tool? :3

All the way..
I am throughout enthusiastic about this work!

And carry on with your work
Intel Core Duo Quad Q6600 4x 2.4 Ghz / Gainward NVidia GeForce 8800 GT 1024MB / 4GigRAM DDR2 1066Mhz
Gigabyte P35-DS3P / Win7 Ultimate

JOIN OUR SERVER!
Tech&Mini.ne
http://www.tm-creative.org

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by f*ckfish » 24 Apr 2008 13:48

SmashingDeluXe wrote::thumbsup:
Hey again nice Work fish!
I test it on my homepage and the response of the user and me is the same!

Awesome! Now it´s for TM User a lot easier to write some colors in forums ect.
But why not update the TM Forum with this tool? :3

All the way..
I am throughout enthusiastic about this work!

And carry on with your work
Thanks a lot =) Although this is not the download you need if you want to include [tm] tags in your forum.
I wrote a bbcode mod for phpBB2, available there: http://fish.oorf.de/index.php?page=3&subpage=98
If someone knew how to write a bbcode mod for phpBB3 it would be easy to include it for TM-Forum or any other phpBB3 forum ;-)
inactive, sorry

moop
pedestrian
pedestrian
Posts: 11
Joined: 27 Oct 2007 18:53
Owned TM-games: TMN

Re: [PHP/HTML] TMF Color Parser

Post by moop » 30 Apr 2008 22:11

Thanks for this, saved me a lot of trouble for parsing recs!

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by f*ckfish » 30 Apr 2008 23:07

You're welcome =)
inactive, sorry

User avatar
niarfman
road tourist
road tourist
Posts: 83
Joined: 05 Jun 2008 07:57
Owned TM-games: TMUF

Re: [PHP/HTML] TMF Color Parser

Post by niarfman » 19 Jun 2008 08:02

Hi,

Your class is very useful, great Job. :thumbsup:

But I wasn't able to find all Tahoma fonts files. I only found tahoma.ttf and tahomabd.ttf ... :oops:
So I have to force font to tahoma.ttf or arialdb.ttf (result is near to the ingame display) to use drawStyledString function.

Where can we download tahomai.ttf, tahomawd.ttf and tahomawdit.ttf files ?

I have tried to found it before asking, but It was useless ... Are they free ?

Thx by advance
Image
Admin en second des serveurs SSM° / tripatouilleur de PHP
Tous nos serveurs tournent sous GNU/Linux !

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by f*ckfish » 19 Jun 2008 08:39

They're included in the download ;-)
inactive, sorry

User avatar
niarfman
road tourist
road tourist
Posts: 83
Joined: 05 Jun 2008 07:57
Owned TM-games: TMUF

Re: [PHP/HTML] TMF Color Parser

Post by niarfman » 19 Jun 2008 08:57

:roflol:

I have downloaded it 3 weeks ago and I certainly deleted it since ...

I have founded them so it's good ^^

I use to work with designers and they never include fonts with their package due to rights (they are using commercials fonts) ... So I didn't think they can be included here. I have searched for nothing XD.

Thanks a lot f**ck fish
Image
Admin en second des serveurs SSM° / tripatouilleur de PHP
Tous nos serveurs tournent sous GNU/Linux !

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by f*ckfish » 19 Jun 2008 09:10

niarfman wrote::roflol:

I have downloaded it 3 weeks ago and I certainly deleted it since ...

I have founded them so it's good ^^

I use to work with designers and they never include fonts with their package due to rights (they are using commercials fonts) ... So I didn't think they can be included here. I have searched for nothing XD.

Thanks a lot f**ck fish
Shhhhhhht :wink:
inactive, sorry

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by f*ckfish » 04 Jul 2008 10:12

New version with just one tiny change is online. The parser now transforms Manialinks into tmtp-calls that are opened inside the gameclient (in version 1.0 they led to smurfs manialink viewer)

Have fun =)
inactive, sorry

User avatar
hal|Sascha
Pit Crew
Pit Crew
Posts: 671
Joined: 12 Aug 2005 16:22
Owned TM-games: TMU, TMN, TMS, TMO
Location: Germany Munich
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by hal|Sascha » 07 Jul 2008 14:05

I have a suggestion, it would be great if the parser could also remove everything or just single tags. I extended the original class to archive this, possibly this is something usefull for an later update :)

Code: Select all

class TextParserClass extends TMFColorParser {
	/**
	* Returns a tmcode formated string
	*
	* @param int $string
	* @param boolean $remove
	* @param boolean $stripColors
	* @param boolean $stripLinks
	* @author hal.sascha
	*/
	public function toHTML($str, $remove = false, $stripColors = false, $stripLinks = false) {
		if($remove === true) { //removes everything
			$str = preg_replace('!\$[iwosntmgzlIWOSNTMGZL]{1}|\$[\d\a-fA-F]{3}!s', '', $str);
		} elseif(is_string($remove)) { //removes single tag, support for an array of tags would be great
			$str = preg_replace('!\$['.strtolower($remove).strtoupper($remove).']{1}!s', '', $str);
		}
		return parent::toHTML($str, $stripColors, $stripLinks);
	}
}
CPU: Intel Core 2 Duo E6600
Mainboard: Asus P5W DH Deluxe
RAM: 2 GB
Graphics: ATI Radeon X1950XTX
Audio: Soundblaster Audigy 4
Internet: ADSL 6Mbit
OS: Windows Vista Bussiness

folen
wheelbarrow operator
wheelbarrow operator
Posts: 15
Joined: 27 Jan 2008 18:49
Owned TM-games: TMU

Re: [PHP/HTML] TMF Color Parser

Post by folen » 19 Jul 2008 17:14

Just thought I'd make you aware of one way in which this differs from the in-game parsing. It's really a border-line case so I wouldn't go so far as calling it a bug, it's more about bugged names :P

The game seems to recognise any tag starting with [0-9][a-f][A-F] and then continuing with two arbitrary characters (not $ though) as a colour code, and if the two additional characters are not "proper" they are seen as "0". E.g. both $foo and $fry will mean the same as $f00. (This is just from experimentation, so may be inaccurate)

Afraid I currently haven't made a proper patch.

Xymph
Pit Crew
Pit Crew
Posts: 5648
Joined: 19 Aug 2007 12:58
Owned TM-games: TMN, TMU, TMF, TM²
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by Xymph » 19 Jul 2008 17:27

folen wrote:The game seems to recognise any tag starting with [0-9][a-f][A-F] and then continuing with two arbitrary characters (not $ though) as a colour code, and if the two additional characters are not "proper" they are seen as "0". E.g. both $foo and $fry will mean the same as $f00. (This is just from experimentation, so may be inaccurate)
Well, there should be a law against such incorrect color codes anyway. :wink:
Developer of XASECO for TMF/TMN ESWC & XASECO2 for TM²: see XAseco.org
Find your way around the Mania community from the TMN ESWC hub, TMF hub, TM² hub, and SM hub

User avatar
f*ckfish
Pit Crew
Pit Crew
Posts: 1302
Joined: 15 Jun 2007 07:59
Owned TM-games: TMNF, TMUF
Location: Dresden, Germany
Contact:

Re: [PHP/HTML] TMF Color Parser

Post by f*ckfish » 19 Jul 2008 18:24

Thanks for the info. I think when I wrote that, there was a reason for that, dunno anymore^^ Anyways I'll maybe fix it later, but it's not top level priority for now =)
inactive, sorry

[SA.fr] Tybos
cyclist
cyclist
Posts: 45
Joined: 19 Jun 2008 11:06
Owned TM-games: TMU, TMUF

Re: [PHP/HTML] TMF Color Parser

Post by [SA.fr] Tybos » 06 Aug 2008 12:31

Hi f*ckfish,

I wanted to thank you very much for your job since I use your TMF Color Parser in many places in my code developing a web interface to manage servers (TMWeb like).

I noticed a little difference between your tool and Trackmania rendering links ($l).
Here is the sample :
In our servers comment, we have put :
$l[www.thesharkteam.com]$999www.$06cthesharkteam.$999com$l
which looks like
www.thesharkteam.com
with this as link : http://www.thesharkteam.com
Trackmania renders it as expected where TMF Color Parser puts this link :
localhost/www.thesharkteam.com

I managed to make both have same behaviour using $l[http://www.thesharkteam.com] but since Trackmania doesn't need an explicit protocol declaration, couldn't your tool do the same ?

It's not a critical bug, but I thought I had to share this finding !

Once again, very good job. :thumbsup:
Regards
[SA.fr] TyBos

Post Reply