Home >Backend Development >PHP Tutorial >PHP color text implementation code_PHP tutorial

PHP color text implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:45:24989browse

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

Copy 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 the font length and width range
$dx = abs($size[2 ]-$size[0]) +10;
$dy = abs($size[5]-$size[3]);
//Build 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 only expresses 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 themselves.
2. Colored word application
The colored words generated by the above program are transmitted through "?t=text", but it needs Note that these texts are best encoded with urlencode. Of course, the length should also be limited. This is beyond 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, transfer it manually.
To use colored words, just use , where color.php is the program that generates colored words (i.e. the above program), xxx is urlencoded text (used to generate colored words)
3.smarty plug-in
Create a new file modifier.ubb in the smarty plugins directory. php, the content is as follows:
Copy 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. For more ubb tags, friends can follow the method themselves To implement, actually modify the color.php base in
return preg_replace($ubb,$tohtml,$string);
}

In this way, to display colored words, just Add
[colorfont] text[/colorfont] to the content. When displaying
, just use the ubb modifier in the smarty template, such as {$contentubb}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320332.htmlTechArticleColored characters are popular recently. Here is a simple implementation method: 1. Simple implementation of colored characters. Copy the code. The code is as follows: header("content-type: image/png"); $text = $_get['t']; $font = 'stxingka.ttf'...
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