Home  >  Article  >  Backend Development  >  What color is the top of the rainbow? PHP color text implementation code

What color is the top of the rainbow? PHP color text implementation code

WBOY
WBOYOriginal
2016-07-29 08:40:191619browse

Colored characters are popular recently, here is a simple implementation method:
1. Simple implementation of color characters

Copy the code The code is as follows:


header("content-type: image/png");
$text = $_get['t'];
$font = 'stxingka.ttf'; //ttf font
$fontsize = 30;
$size = imagettfbbox($fontsize, 0, $font, $text); //get Font length and width range
$dx = abs($size[2]-$size[0]) +10;
$dy = abs($size[5]-$size[3]);
//Construct image
$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 is just It explains some basic principles of color words. To achieve more complex and beautiful color words, all you need to do is to change the font, change the font color, add some background images, and then consider caching, etc. The method is similar. Friends can Try it yourself.
2. Color word application
The color words generated by the above program are transmitted through "?t=text", but it should be noted that these words are best encoded with urlencode. Of course, the length should also be There are limitations, this is not the scope of this article.
In addition, the program that generates colored words and the program that transmits text both use utf-8 encoding. If not, convert it manually..
To use colored words, just use That's it, where color.php is the program that generates colored words (that is, the program above), and xxx is the text encoded by urlencode (used to generate colored words)
3. smarty plug-in
Create a new file modifier.ubb.php in smarty’s plugins directory with the following content:

Copy the code The code is as follows:


function smarty_modifier_ubb($string){
$ubb = array(
'/[b](.+?)[/b]/i', #bold
'/[url=(.+?)](.+?)[/url]/i', #url
'/[colorfont](.+?)[/colorfont]/ie' #Color words, please add the e modifier
);
$tohtml = array(
'\1' ,
'\2',
'""'
);
//The above is just the implementation of ubb. Friends can implement more ubb tags by themselves according to the method. The color.php base is actually modified
return preg_replace($ubb,$tohtml, $string);
}


In this way, to display color words, just add
[colorfont] text[/colorfont] to the content. When displaying, use the ubb modifier in the smarty template, such as {$contentubb }
The above introduces the PHP color text implementation code of what color is the top of the rainbow, including what color is the top of the rainbow. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn