PHP color text implementation code_PHP tutorial
c
header
php
one
under
code
copy
accomplish
color
letter
method
yes
of
Simple
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
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}
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks ago
By DDD

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
