Home >Backend Development >PHP Tutorial >What color is the top of the rainbow? PHP color text implementation code
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);
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);
}