帝国CMS背景可自定义,可单独背景也可以随机背景,此插件根帝国cms官方论坛帖子改的,增加了生成图片后写入数据库,笔者的古诗词网 www.gushichi.com 也是这样设置的。
效果图
将下面的代码插入到/e/class/userfun.php中增加如下函数
单独背景代码
//自动生成图片
function Cyimg($classid,$id,$width='512',$height='340',$bg='',$path='d/file/titlepic/'){global $dbtbpre,$empire,$class_r,$public_r;$bg = ECMS_PATH.'/bazhepu/images/titlepicbg.png';//背景图片存放的路径 指定一张图片$tbname = $class_r[$classid]['tbname'];$r=$empire->fetch1("select * from {$dbtbpre}ecms_{$tbname} where id='$id' limit 1");$mycr=GetPathname($class_r[$r['classid']]['classpath']);//栏目目录$classpath=$mycr[0];//获取栏目目录$path = $path.$classpath.$r['id'].'.png';//保存的图片名称$file = ECMS_PATH.$path;//图片的绝对地址if(!file_exists($file)){//判断图片是否存在 测试的时候把!去掉 正式使用时加上就可以了$font = ECMS_PATH."/bazhepu/font/PingFangMedium.ttf";//你需要的字体路径$myImage=imagecreatefrompng($bg);//背景图片$blue=imagecolorallocate($myImage, 210, 95, 50);//设置字体颜色$fontBox = imagettfbbox(55, 0, $font ,$r['title']);//获取文字所需的尺寸大小$width = ceil(($width - $fontBox[2]) / 2) + 20;//计算文字所需宽度 居中 后面加的数字表示偏移多少$height = ceil(($height - $fontBox[1] - $fontBox[7]) / 2) - 35;//计算文字所需高度 居中 后面加的数字表示偏移多少imagettftext($myImage, 48, 0, $width, $height, $blue, $font, $r['title']); //生成图片imagepng($myImage,$file); //保存图片imagedestroy($myImage);//释放内存// 更新数据$empire->query("update {$dbtbpre}ecms_{$tbname} set titlepic = '".$path."' where id= '$id'");}return $public_r['newsurl'].$path;//返回图片路径
}
随机背景代码
function Cyimg($classid,$id,$width='512',$height='340',$bg='',$path='d/file/titlepic/'){global $dbtbpre,$empire,$class_r,$public_r;$bgnum = rand(1,9);//随机数字图片名字$bg = ECMS_PATH.'/bazhepu/images/'.$bgnum.'.png';//背景图片存放的路径 随机多张图片$tbname = $class_r[$classid]['tbname'];$r=$empire->fetch1("select * from {$dbtbpre}ecms_{$tbname} where id='$id' limit 1");$mycr=GetPathname($class_r[$r['classid']]['classpath']);//栏目目录$classpath=$mycr[0];//获取栏目目录$path = $path.$classpath.$r['id'].'.png';//保存的图片名称$file = ECMS_PATH.$path;//图片的绝对地址if(!file_exists($file)){//判断图片是否存在 测试的时候把!去掉 正式使用时加上就可以了$font = ECMS_PATH."/bazhepu/font/PingFangMedium.ttf";//你需要的字体路径$myImage=imagecreatefrompng($bg);//背景图片$blue=imagecolorallocate($myImage, 210, 95, 50);//设置字体颜色$fontBox = imagettfbbox(55, 0, $font ,$r['title']);//获取文字所需的尺寸大小$width = ceil(($width - $fontBox[2]) / 2) + 20;//计算文字所需宽度 居中 后面加的数字表示偏移多少$height = ceil(($height - $fontBox[1] - $fontBox[7]) / 2) - 35;//计算文字所需高度 居中 后面加的数字表示偏移多少imagettftext($myImage, 48, 0, $width, $height, $blue, $font, $r['title']); //生成图片imagepng($myImage,$file); //保存图片imagedestroy($myImage);//释放内存// 更新数据$empire->query("update {$dbtbpre}ecms_{$tbname} set titlepic = '".$path."' where id= '$id'");}return $public_r['newsurl'].$path;//返回图片路径
}
内容页调用代码
<img src="<?=Cyimg($navinfor['classid'],$navinfor['id'])?>" />