如图,我在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();
我是新手_请多指教