PHP color text implementation code

Source: Internet
Author: User
The following is a simple implementation code. If you like php, you can check it out. The following is a simple implementation method for the recent pop-up:
1. simple implementation of color words

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]);
// Construct the 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 the basic principles of some color words. to implement more complex and beautiful color words, all you have to do is change the font, change the font color, and add some background images, consider caching and so on, and the method is similar. you can try it on your own.
2. color text application
The color text generated by the above program is "? T = text "to pass, but note that it is best to use urlencode to encode these words. of course, the length should also be limited, which is not the scope discussed in this article.
In addition, the program that generates the color text and the program that transmits the text use UTF-8 encoding. if not, manually repeat it ..
To use the color word, you only need to use it. among them, the color. php program (that is, the above program), xxx is the text encoded by urlencode (used to generate the color word)
III. smarty plug-in
Create a new file modifier. ubb. php in the smarty plugins directory. the content is as follows:

The code is as follows:


Function smarty_modifier_ubb ($ string ){
$ Ubb = array (
'/\ [B \] (. + ?) \ [\/B \]/I ', # Bold
'/\ [Url = (. + ?) \] (. + ?) \ [\/Url \]/I ', # url
'/\ [Colorfont \] (. + ?) \ [\/Colorfont \]/ie '# Color. Note that the e modifier must be added.
);
$ Tohtml = array (
'\ 1',
'\ 2 ',
'""'
);
// The above is just the implementation of ubb. more ubb tag friends can implement it by themselves, and the color. php root handler is actually modified.
Return preg_replace ($ ubb, $ tohtml, $ string );
}


In this way, to display the color, you only need to add
[Colorfont] Text [/colorfont]
Use the ubb modifier in the smarty template, for example, {$ 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.