php 头条行 代码,phpcms头条图片

[php]代码库是图片格式的.

使用方法为:

在模板里需要显示的地方插入:

view sourceprint?

1.{$r['title']}

核心就是将头条文字传递给函数 toptitle() . 该函数代码如下:

view sourceprint?

01./**

02.* toptitle函数说明

03.* 头条文字生成为图片,返回图片路径,使用输出

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 的定义,或者传入函数的时候指明字体文件路径都可以.

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值