Heim  >  Artikel  >  php教程  >  PHP 彩色文字实现代码

PHP 彩色文字实现代码

WBOY
WBOYOriginal
2016-06-13 12:22:421037Durchsuche

最近流行彩字,下面是简单的实现方法:
一.彩字的简单实现

复制代码 代码如下:


header("content-type: image/png");
$text = $_get['t'];
$font = 'stxingka.ttf'; //ttf字体
$fontsize = 30;
$size = imagettfbbox($fontsize, 0, $font, $text); //获得字体长宽范围
$dx = abs($size[2]-$size[0]) +10;
$dy = abs($size[5]-$size[3]);
//构建图像
$im = imagecreate($dx,$dy);
imagecolorallocate($im, 255,255, 255); //背景色
$fontcolor = imagecolorallocate($im, 255, 0, 0); //字体颜色
imagettftext($im, $fontsize, 0, 0, abs($size[5]), $fontcolor, $font, $text);
imagepng($im);
imagedestroy($im);


上面的程序只是表述了一些彩字的基本原理,要实现更复杂和美观的彩字,所要做的只是更换一下字体,改一下字体颜色,添加一些背景图,再考虑一下缓存等,方法也差不多,朋友们可以自己试试.
二.彩字应用
上面的程序生成的彩字是通过"?t=文字"来传递的,但需注意的是,这些文字最好用urlencode来编码,当然,长度也应该有限制,这不是本文讨论的范围.
另外,生成彩字的程序和传递文字的程序都使用utf-8编码,如果不是,手工转一下..
要使用彩字,只需要用PHP 彩色文字实现代码即可,其中,color.php为生成彩字的程序(即上面的程序),xxx为经urlencode编码的文字(用来生成彩字)
三.smarty插件
在smarty的plugins目录下新建一文件modifier.ubb.php,内容如下: 

复制代码 代码如下:


function smarty_modifier_ubb($string){
$ubb = array(
'/\[b\](.+?)\[\/b\]/i', #加粗
'/\[url=(.+?)\](.+?)\[\/url\]/i', #url
'/\[colorfont\](.+?)\[\/colorfont\]/ie' #彩字,注意,要加e修饰符
);
$tohtml = array(
'\\1',
'\\2',
'"PHP 彩色文字实现代码"'
);
//以上只是演ubb的实现,更多的ubb标签朋友们可以按方法自己实现,其中的color.php根椐实际去修改
return preg_replace($ubb,$tohtml,$string);
}


这样,要显示彩字,只需在内容中加入
[colorfont]文字[/colorfont]
显示时,在smarty模板中使用ubb修饰符即可,如{$contentubb}
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP 七大优势分析Nächster Artikel:php 静态页面中显示动态内容