PHP implementation of color text

Source: Internet
Author: User
Tags abs array modifier urlencode smarty template
Recently popular color words, the following is a simple implementation method:

Recently popular color words, the following is a simple implementation method:

A simple realization of the color word

The following are the referenced contents:
Header ("Content-type:image/png");
$text = $_get[' t '];
$font = ' Stxingka.ttf '; TTF fonts
$fontsize = 30;
$size = Imagettfbbox ($fontsize, 0, $font, $text); Get font 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 expresses some of the basic principles of color words, 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 caching, and so on, the method is similar, friends can try their own.

Two. Color Word application

The above program produces the color word through "t= text" to pass, but should note that these words are best to use UrlEncode to encode, of course, the length should also have limitations, this is not the scope of this article discussed.
In addition, generate the color Word program and the program that transmits the text uses UTF-8 code, if not, manual turn.
To use the color Word, only need to use can, of which, color.php to generate color Word program (that is, the above program), XXX for UrlEncode encoded text (used to generate color words)

Three. Smarty Plugin

Create a new file modifier.ubb.php in the plugins directory of Smarty, which reads as follows:

The following are the referenced contents:
function Smarty_modifier_ubb ($string) {
$ubb = Array (
'/\[b\] (. +?) \[\/b\]/i ', #加粗
'/\[url= (. +?) \](.+?) \[\/url\]/i ', #url
'/\[colorfont\] (. +?) \[\/colorfont\]/ie ' #彩字, note, to add the E modifier
);
$tohtml = Array (
' <b>\\1</b> ',
' <a href= ' \\1 ' >\\2</a> ',
' " '
);
The above is just the implementation of UBB, more UBB tag friends can be achieved by way of their own, where the color.php root according to the actual to modify
Return Preg_replace ($ubb, $tohtml, $string);
}


So, to display the color Word, just add it to the content
[Colorfont] text [/colorfont]
When displayed, use the UBB modifier in the smarty template, such as {$contentubb}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.