详情页

帝国CMS字段转换为图片 支持中文英文数字生成

时间:2024年03月30日

编辑:佚名

帝国CMS任意字段转换为图片插件,支持中文英文数字生成,文章标题,电话号码、QQ号码均可,
使用方法:
一、将以下代码存为index.php
<?php
mb_internal_encoding("UTF-8");//设置编码
header ('Content-type:image/png');
require('../../class/connect.php');//引入数据库配置文件和公共函数文件
require('../../class/db_sql.php');//引入数据库操作文件
$link=db_connect();//连接MYSQL
$empire=new mysqlquery();//声明数据库操作类
$editor=1;//声明目录层次
$classid=(int)$_GET['classid'];
$id=(int)$_GET['id'];
$area=addslashes($_GET['area']);
$r1=$empire->fetch1("select tbname from {$dbtbpre}enewsclass where classid=$classid");
$tbname=$r1['tbname'];
$r=$empire->fetch1("select $area from {$dbtbpre}ecms_$tbname where classid=$classid and id=$id");
/*16进制颜色转换为RGB色值*/
function hex2rgb($hexColor){
$color=str_replace('#','',$hexColor);
if(strlen($color)>3){
$rgb = array('r'=>hexdec(substr($color,0,2)),'g'=>hexdec(substr($color,2,2)),'b'=>hexdec(substr($color,4,2)));
}else{
$color=str_replace('#','',$hexColor);
$r=substr($color,0,1).substr($color,0,1);
$g=substr($color,1,1).substr($color,1,1);
$b=substr($color,2,1).substr($color,2,1);
$rgb=array('r'=>hexdec($r),'g'=>hexdec($g),'b'=>hexdec($b));
}
return $rgb;
}
/*各种参数*/
$width=(int)$_GET['w']?(int)$_GET['w']:300;
$hegih=(int)$_GET['h']?(int)$_GET['h']:50;
$ff=$_GET['ff']?$_GET['ff']:'msyh';
$fontface='font/'.$ff.'.ttf';
$fontsize=$_GET['fz']?$_GET['fz']:12;
$bcolor=$_GET['bc']?$_GET['bc']:'fff';
$tcolor=$_GET['tc']?$_GET['tc']:'000';
$b=hex2rgb($bcolor);//背景色
$t=hex2rgb($tcolor);//文字色
/*中英文换行等处理*/
function str_to_image($fontsize,$angle,$fontface,$string,$width){//这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
$content="";//将字符串拆分成一个个单字 保存到数组 letter 中
for($i=0;$i<mb_strlen($string);$i++){
$letter[]=mb_substr($string,$i,1);
}
foreach($letter as $l){
$teststr=$content." ".$l;
$testbox=imagettfbbox($fontsize,$angle,$fontface,$teststr);
//判断拼接后的字符串是否超过预设的宽度
if (($testbox[2]>$width)&&($content!=="")){
$content.="\n";
}
$content.=$l;
}
return $content;
}
$bg=imagecreate($width,$hegih);//创建白色画布
$background_color=imagecolorallocate($bg,$b['r'],$b['g'],$b['b']);//画布背景颜色 
$text_color=imagecolorallocate($bg,$t['r'],$t['g'],$t['b']);//内容文字颜色
/**/
if($r[area]=''){
$text='No Data';
}else{
$text=stripslashes($r[$area]);
}
$text=str_to_image($fontsize,0,$fontface,$text,$width-10);//自动换行处理
//若文件编码为 GB2312 请将下行的注释去掉
//$text = iconv("GB2312", "UTF-8", $text);
imagettftext($bg,$fontsize,0,10,20,$text_color,$fontface,$text);//输出图像
imagepng($bg);
imagedestroy($bg);
db_close();//关闭MYSQL链接
$empire=null;//注消操作类变量
?>  
二、将index.php放在/e/extend/topic/下
三、在要显示图片字段的地方加入以下代码即可
<img src="[!--news.url--]e/extend/topic/index.php?classid=[!--classid--]&id=[!--id--]&w=500&h=100&area=title&ff=msyh&fz=12&bc=fff&tc=000" />
复制代码
参数说明:
classid=信息所属栏目ID
id=信息ID
w=宽 
h=高
area=你的字段(如:qq)
fz=字号大小
bc=图片背景颜色 (颜色为16进制代码)
tc=显示文字颜色 (颜色为16进制代码)
ff=字体名称,ttf格式的,放在/e/extend/topic/font/下。
font目录下为你使用的字体存放,这个为必须。
相关文章
猜你需要