最新公告
  • 欢迎您光临 我爱模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境! 立即加入钻石VIP
  • 帝国CMS自动生成标题图片并写进数据库

    正文概述 管理员   2025-09-04   10

    背景可自定义,可单独背景也可以随机背景

    此插件根据帝国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'])?>" />


    我爱模板网 » 帝国CMS自动生成标题图片并写进数据库

    发表评论

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者
    script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?587cc1e5c052b5b0ce99533beff13c96"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

    请选择支付方式

    ×
    支付宝支付
    微信支付
    余额支付
    ×
    微信扫码支付 0 元