Recent popular color words, below is the simple implementation method:
I. Simple implementation of color words
Copy the Code code as follows:
Header ("Content-type:image/png");
$text = $_get[' t '];
$font = ' Stxingka.ttf '; TTF Font
$fontsize = 30;
$size = Imagettfbbox ($fontsize, 0, $font, $text); Get the font length and width range
$DX = ABS ($size [2]-$size [0]) +10;
$dy = ABS ($size [5]-$size [3]);
Building images
$im = Imagecreate ($DX, $dy);
Imagecolorallocate ($im, 255,255, 255); Background color
$fontcolor = Imagecolorallocate ($im, 255, 0, 0); Font Color
Imagettftext ($im, $fontsize, 0, 0, ABS ($size [5]), $fontcolor, $font, $text);
Imagepng ($im);
Imagedestroy ($im);
The above program just expressed some of the basic principles of color, to achieve more complex and beautiful color words, all you have to do is change the font, change the font color, add some background map, and then consider the cache, and so on, the method is similar, friends can try their own.
Two. Color Word application
The above program generated color words through the "? t= text" To pass, but note that these words are best encoded with urlencode, of course, the length should also be limited, this is not the scope of this article discussed.
In addition, the generation of color-coded programs and text-passing programs use UTF-8 encoding, if not, manually turn around.
To use color words, just need to use, wherein, color.php for the generation of color Word program (ie, the above program), XXX for the UrlEncode encoded text (used to generate color words)
Three. Smarty Plug-in
Create a new file modifier.ubb.php under Smarty's plugins directory, as follows:
Copy the Code code as follows:
function Smarty_modifier_ubb ($string) {
$ubb = Array (
'/\[b\ ' (. +?) \[\/b\]/i ', #加粗
'/\[url= (. +?) \](.+?) \[\/url\]/i ', #url
'/\[colorfont\ ' (. +?) \[\/colorfont\]/ie ' #彩字, note that the E modifier is to be added
);
$tohtml = Array (
'\\1',
' \\2 ',
'""'
);
The above is only the implementation of UBB, more UBB tag friends can be implemented by their own methods, of which the color.php root to modify the actual
Return Preg_replace ($ubb, $tohtml, $string);
}
In this way, to display the color Word, simply add in the content
[Colorfont] text [/colorfont]
When displayed, use the UBB modifier in the smarty template, such as {$contentubb}
The above on the rainbow top is what color PHP color text implementation code, including the color of the top of the rainbow content, I hope the PHP tutorial interested in a friend helpful.