是图片格式的. |
使用方法为: |
在模板里需要显示的地方插入: |
view sourceprint? |
1.<img src= "{toptitle($r['title'],'28','',880,50)}" alt= "{$r['title']}" /> |
核心就是将头条文字传递给函数 toptitle() . 该函数代码如下: |
view sourceprint? |
01. /** |
02.* toptitle函数说明 |
03.* 头条文字生成为图片,返回图片路径,使用<img src="" />输出 |
04.* @title 文字内容; |
05.* @fontsize 文字字体; |
06.* @w 宽度;h 高度 |
07.* @字体路径 |
08.*/ |
09. function toptitle( $title , $fontsize , $fontpath , $w = 880, $h = 50) { |
10. include_once ( 'dir.func.php' ); |
11. $title = iconv( 'gbk' , 'utf-8' , $title ); |
12. $fontsize = $fontsize ? $fontsize :28; |
13. $savepath = 'caches/caches_titleimg/' . $fontsize . '/' ; //路径名 |
14. $imgname = md5( $title ). '.png' ; //图片名称规则 |
15. $titlepath = PHPCMS_PATH. $savepath . $imgname ; //图片的绝对路径 |
16. if ( file_exists ( $titlepath )) return APP_PATH. $savepath . $imgname ; //图片已存在直接返回 |
17. if (! is_dir ( $savepath )) dir_create(PHPCMS_PATH. $savepath ); //路径不存在则创建路径 |
18. $fontpath = $fontpath ? $fontpath :(PC_PATH. 'libs' .DIRECTORY_SEPARATOR. 'data' .DIRECTORY_SEPARATOR. 'font' .DIRECTORY_SEPARATOR. 'yaheicuti.ttf' ); |
19. $im = imagecreate( $w , $h ); |
20. $white = imagecolorallocate( $im ,255,255,255); |
21.imagecolortransparent( $im , $white ); |
22. $black = imagecolorallocate( $im ,9,84,139); |
23.imagettftext( $im , $fontsize ,0,0,40, $black , $fontpath , $title ); //字体路径 |
24.header( "Content-type:image/png" ); |
25.imagepng( $im , $titlepath ); |
26.ImageDestroy( $im ); |
27. return APP_PATH. $savepath . $imgname ; |
28.} |
将以上函数代码写到 phpcms\libs\functions\extention.func.PHP 里面. 然后将字体文件yaheicuti.ttf放到phpcms\libs\data\font\ 里面. 字体较大,请前往 http: //tuzwu.javaeye.com/blog/782473 下载.当然你可以换成你自己的字体,可以通过修改函数里的 $fontpath 的定义,或者传入函数的时候指明字体文件路径都可以. |