加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php使用imagettftext()函数有干扰线但是没有文字的问题解决

发布时间:2020-12-13 17:32:38 所属栏目:PHP教程 来源:网络整理
导读:public function code() { // 主要参数 if ( $font_size == 0) $font_size = 20 ; if ( $img_width == 0) $img_width = 110 ; if ( $img_height == 0) $img_height = 50 ; if ( $word_type == 0) $word_type = 3; // 1:数字 2:英文 3:混合 $font_file = ‘E:
 public function code()
    {

        //主要参数
        if($font_size == 0) $font_size = 20;
        if($img_width == 0) $img_width = 110;
        if($img_height == 0) $img_height = 50;
        if($word_type == 0) $word_type = 3;   // 1:数字 2:英文 3:混合
        $font_file  = ‘E:phpStudyPHPTutorialWWWthinkphp_3.2.3_fullApplicationHomeControllerSIDESHOW.TTF‘;//字体的路径
        
        //创建图片,并设置背景色
        $im = @imagecreate($img_width,$img_height);
        imagecolorallocate($im,192,192);
        
        //获取随机字符
        if($word_type == 1) {
            $verifyCode = implode(‘‘,range(2,9));
        }elseif ($word_type == 2) {
            $verifyCode = implode(‘‘,range(‘A‘,‘Z‘));
        }else{
            $verifyCode = implode(‘‘,array_merge(range(2,9),‘Z‘)));
            $verifyCode = str_replace(array(‘I‘,‘O‘),array(‘P‘,‘N‘),$verifyCode);
        }
        //打乱字符串
        $verifyCode = str_shuffle($verifyCode);
        $rndstring = substr($verifyCode,4);
        //echo $rndstring;exit;
        
        $rndcodelen = strlen($rndstring);
        
        //干扰线
        for($i = 0; $i < 5; $i++) {
            $color = imagecolorallocate($im,0);
            imageline($im,rand(0,$img_width),$img_height),$color);
        }
        
        //画边框
        //$bordercolor = imagecolorallocate($im,0);
        //imagerectangle($im,$img_width-1,$img_height-1,$bordercolor);
        
        //输出文字
        for($i = 0;$i < $rndcodelen;$i++){
            $rndstring[$i] = strtoupper($rndstring[$i]);
            $c_fontColor = imagecolorallocate($im,0);
            $y_pos = $i == 0 ? 10 : $i * ($font_size + 8);
            $c = mt_rand(0,15);
            imagettftext($im,$font_size,$c,$y_pos,35,$c_fontColor,$font_file,$rndstring[$i]);
        }
        header("Pragma:no-cachern");
        header("Cache-Control:no-cachern");
        header("Expires:0rn");
        if(function_exists("imagejpeg")){
            header("content-type:image/jpegrn");
            imagejpeg($im);
        }else{
            header("content-type:image/pngrn");
            imagepng($im);
        }
        imagedestroy($im);
        exit();
    }

生成的是这样的,原因在于字体的路径加载不道,红色部分改成自己服务器的路径

更改了字体的路径以后? 就可以正常显示了

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读