一、实现方法1)标题图片字体定义
首先上传字体文件上传到include/data/fonts/下,并命名为 STXINGKA.TTF (字体下载),这个是用于生成标题图片的字体的。当然,你也可以自己定义更漂亮的字体,但字体必须命名为STXINGKA.TTF。
2)标题图片函数文件
打开include/extend.func.php (注意:本文件为自定义函数接口,如果你没有这个文件,请自己创建一个),在最后面的?>的前面以上开始插入以下代码:
折叠展开PHP 代码复制内容到剪贴板
- class?DeDeTitle ??
- { ??
- ????var?$text?=?'暂无标题'; ??
- ????var?$bg?=?'#F0F0F0'; ??
- ????var?$color?=?'#30F'; ??
- ????var?$width?=?500; ??
- ????var?$height?=?50; ??
- ????var?$size?=?30; ??
- ????var?$font; ??
- ????var?$path; ??
- ????function?__construct($t) ??
- ????{ ??
- ????????if($t) ??
- ????????{ ??
- ????????????$this->text?=?$t; ??
- ????????} ??
- ????????$this->font?=?DEDEINC.'/data/fonts/STXINGKA.TTF'; ??
- ????} ??
- ????function?DeDeTitle($t) ??
- ????{ ??
- ????????$this->__construct($t); ??
- ????} ??
- ????function?TextConv() ??
- ????{ ??
- ????????global?$cfg_soft_lang; ??
- ????????if($cfg_soft_lang!='utf-8') ??
- ????????{ ??
- ????????????return?gb2utf8($this->text); ??
- ????????} ??
- ????????return?$this->text; ??
- ????} ??
- ????function?C($color,$rgb) ??
- ????{??? ??
- ????????if(strlen($color)==4) ??
- ????????{ ??
- ????????????for($i=1;$i<4;$i++) ??
- ????????????{ ??
- ????????????????$str?.=?str_repeat($color[$i],2); ??
- ????????????} ??
- ????????????$color?=?'#'.$str; ??
- ????????} ??
- ????????switch($rgb) ??
- ????????{ ??
- ????????????case?'R':????return?hexdec(substr($color,?1,?2)); ??
- ????????????case?'B':????return?hexdec(substr($color,?3,?2)); ??
- ????????????case?'G':????return?hexdec(substr($color,?5,?2)); ??
- ????????} ??
- ????} ??
- ????function?Titlepath() ??
- ????{ ??
- ????????global?$cfg_medias_dir; ??
- ????????$name?=?GetPinyin($this->text).'.gif'; ??
- ????????$path?=?$cfg_medias_dir.'/title/'; ??
- ????????if(!is_dir($path)) ??
- ????????{ ??
- ????????????CreateDir($path); ??
- ????????} ??
- ????????return?$path.$name; ??
- ????} ??
- ????function?Show() ??
- ????{ ??
- ????????if(!file_exists(DEDEROOT.$this->Titlepath())) ??
- ????????{ ??
- ????????????$this->Make(); ??
- ????????} ??
- ????????global?$cfg_cmsurl; ??
- ????????return??'<img?src="'.$cfg_cmsurl.$this->Titlepath().'"/>'; ??
- ????} ??
- ????function?Make() ??
- ????{ ??
- ????????$image?=?imagecreatetruecolor($this->width,$this->height); ??
- ????????$bg?=?ImageColorAllocate($image,$this->C($this->bg,'R'),'B'),'G')); ??
- ????????$color?=?ImageColorAllocate($image,$this->C($this->color,'G')); ??
- ????????ImageFilledRectangle($image,$this->width,$this->height,$bg); ??
- ????????imagettftext($image,$this->size,?0,?$this->size/2,?($this->height+$this->size)/2,?$color,?$this->font,$this->TextConv()); ??
- ????????imagegif($image,DEDEROOT.$this->Titlepath()); ??
- ????????imagedestroy($image); ??
- ????} ??
- }??
3)调用方法简单说明
实际上我们知道,在织梦(DedeCMS)系统中,处理底层字段,一般来说有两种方法:
- function
- runphp
这两种方法的知识不在本教程讲解的重点,大家可查阅相关资料了解!
我们这里的标题图片,运用的就是第二种runphp来处理的,调用方法为:
在arclist标签中:
[field:title runphp='yes']
$title = new DeDeTitle(@me);//使用DedeTile类
$title->bg = '#F0F0F0';//标题图片背静颜色
$title->color = '#30F';//标题图片字体颜色
$title->width = 350;//标题图片的宽度
$title->height = 50;//标题图片的高度
$title->size = 25;//标题图片的字体大小
@me=$title->Show();//显示图片
[/field:title]
如果在是内容页运用,只需要将[field:title/]换成 {dede:field.title/}即可,例如:
{dede:field.title runphp='yes'}
$title = new DeDeTitle(@me);//使用DedeTile类
$title->bg = '#F0F0F0';//标题图片背静颜色
$title->color = '#30F';//标题图片字体颜色
$title->width = 350;//标题图片的宽度
$title->height = 50;//标题图片的高度
$title->size = 25;//标题图片的字体大小
@me=$title->Show();//显示图片
{/dede:field.title/}
二、效果预览
我们在后台的【模板】——[全局标记测试]中输入以下代码:
{dede:arclist row='1' typeid=''}
<a href="[field:arcurl/]">[field:title runphp='yes']
$title = new DeDeTitle(@me);
$title->bg = '#F0F0F0';
$title->color = '#30F';
$title->width = 350;
$title->height = 50;
$title->size = 25;
@me=$title->Show();
[/field:title]</a>
{/dede:arclist}
?
后,看到了如下的效果:
?
怎么样?是不是很酷?赶快尝试吧。
三、其它说明
这里的这个是一个现成的库,他不仅仅可以生成标题图片,也可以生成一些其它的内容。只要你会使用本函数中所介绍的几个参数即可。
dede生成新闻页头部
{dede:arclist row='1' flag='h' titlelen=42}
<a href="[field:arcurl/]">[field:title runphp='yes']
$title = new DeDeTitle(@me);
$title->bg = '#FFFFFF';
$title->color = '#000';
$title->width = 710;
$title->height = 50;
$title->size = 25;
@me=$title->Show();
[/field:title]</a>
{/dede:arclist}
<!--生成flash-->
<script language='javascript'> linkarr = new Array(); picarr = new Array(); textarr = new Array(); var swf_width=400; var swf_height=245; //文字颜色|文字位置|文字背景颜色|文字背景透明度|按键文字颜色|按键默认颜色|按键当前颜色|自动播放时间|图片过渡效果|是否显示按钮|打开方式 var configtg='0xffffff|1|#0A0262|5|0xffffff|0xC5DDBC|0x000033|5|3|1|_blank'; var files = ""; var links = ""; var texts = ""; //这里设置调用标记 {dede:arclist flag='f' row='5'titlelen=50} linkarr[[field:global.autoindex/]] = "[field:arcurl/]"; picarr[[field:global.autoindex/]]? = "[field:litpic/]"; textarr[[field:global.autoindex/]] = "[field:title function='html2text(@me)'/]"; {/dede:arclist} for(i=1;i<picarr.length;i++){ if(files=="") files = picarr[i]; else files += "|"+picarr[i]; } for(i=1;i<linkarr.length;i++){ if(links=="") links = linkarr[i]; else links += "|"+linkarr[i]; } for(i=1;i<textarr.length;i++){ if(texts=="") texts = textarr[i]; else texts += "|"+textarr[i]; } document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0" width="'+ swf_width +'" height="'+ swf_height +'">'); document.write('<param name="movie" value="{dede:global.cfg_templeturl /}/default/images/bcastr3.swf"><param name="quality" value="high">'); document.write('<param name="menu" value="false"><param name=wmode value="opaque">'); document.write('<param name="FlashVars" value="bcastr_file='+files+'&bcastr_link='+links+'&bcastr_title='+texts+'&bcastr_config='+configtg+'">'); document.write('<embed src="{dede:global.cfg_templeturl /}/default/images/bcastr3.swf" wmode="opaque" FlashVars="bcastr_file='+files+'&bcastr_link='+links+'&bcastr_title='+texts+'&bcastr_config='+configtg+'&menu="false" quality="high" width="'+ swf_width +'" height="'+ swf_height +'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'); document.write('</object>'); </script>