为了账号安全,请及时绑定邮箱和手机立即绑定

在新的文件中调用函数报错,但在定义函数的文件中可用

在新的文件中调用函数报错,但在定义函数的文件中可用

PHP
Kit_G 2017-04-26 01:38:44
如图,我在image.func.php中输出getCaptcha();是能够正常显示验证码的但是,我在新建的getCaptcha.php中调用image.func.php再使用getCaptcha()的时候却报错了,这是为什么??image.func.php中的代码:<?php     function getCaptcha($type = 3,$length = 4,$pixel = 30,$line = 0){ session_start();     //创建底图     $width = 80;     $height = 30;     $image = imagecreatetruecolor($width, $height);     $white = imagecolorallocate($image, 255, 255, 255);     $black = imagecolorallocate($image, 0, 0, 0);     //填充画布     //imagefilledrectangle($image, 1, 1, $width-2, $height-2, $white);     imagefill($image, 0, 0, $white);          //获取验证码字段,储存在session中     $type = 3;     $length = 4;     $chars = buildRandomString($type,$length);     $sess_name = "captcha";     $_SESSION[$sess_name] = $chars;     $fontfiles = array("arial.ttf","ariblk.ttf","simkai.ttf","SIMLI.TTF","simsun.ttc","STLITI.TTF");     for ($i=0; $i < $length; $i++) {       $fontsize = mt_rand(12,18);      $angle = mt_rand(-15,15);      $x = ($i*15) + mt_rand(2,5);      $y = mt_rand(15,25);      $color = imagecolorallocate($image, mt_rand(50,150), mt_rand(50,150), mt_rand(50,150));      $font = "../fonts/" . $fontfiles[mt_rand(0,count($fontfiles)-1)];      $text = substr($chars, $i, 1);      imagettftext($image, $fontsize, $angle, $x, $y, $color, $font, $text);          }          //创建干扰元素     if($pixel){     for ($i=0; $i < $pixel; $i++) {       $jamColor = imagecolorallocate($image, mt_rand(100,220), mt_rand(100,220), mt_rand(100,220));      imagesetpixel($image, mt_rand(0,$width-1), mt_rand(0,$height-1), $jamColor);     } } if($line){ for ($i=0; $i < $line; $i++) {  $jamColor = imagecolorallocate($image, mt_rand(100,220), mt_rand(100,220), mt_rand(100,220)); imageline($image, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $jamColor); } } ob_clean(); header('content-type:image/png'); imagepng($image); imagedestroy($image); } getCaptcha();getCaptcha.php的代码:<?php     require_once 'lib/image.func.php';     getCaptcha();
查看完整描述

3 回答

?
我是新手_请多指教

TA贡献140条经验 获得超79个赞

imagettftext中的$font参数无效, 也就是$font = "../fonts/" . $fontfiles[mt_rand(0,count($fontfiles)-1)]得到的结果无效。你新文件中有没建个文件夹放相对应字体的文件"arial.ttf","ariblk.ttf","simkai.ttf","SIMLI.TTF","simsun.ttc","STLITI.TTF"这些

查看完整回答
反对 回复 2017-04-26
  • Kit_G
    Kit_G
    有啊,我在image.func.php中使用这个函数是正常的。但是我在getCaptcha.php中required_once 'image.func.php';再使用这个函数,就报错了。有点搞不懂
  • 3 回答
  • 0 关注
  • 1302 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信