帝国CMS自动生成标题图片并写进数据库

帝国CMS背景可自定义,可单独背景也可以随机背景,此插件根帝国cms官方论坛帖子改的,增加了生成图片后写入数据库,笔者以古诗文网 www.gushichi.com为例,告诉大家帝国CMS自动生成标题图片并写进数据库的具体步骤。

效果图

将下面的代码插入到/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'])?>" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值