[PHP] phpcms头条图片 →→→→→进入此内容的聊天室

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

回复 "phpcms头条图片"

这儿你可以回复上面这条便签

captcha