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

如图,我在image.func.php中输出getCaptcha();是能够正常显示验证码的http://img.mukewang.com/58ff88120001aa9610600568.jpg

但是,我在新建的getCaptcha.php中调用image.func.php再使用getCaptcha()的时候却报错了,这是为什么??

http://img.mukewang.com/58ff887000017a6218900840.jpg

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();


Kit_G
浏览 1303回答 3
3回答

我是新手_请多指教

imagettftext中的$font参数无效, 也就是$font = "../fonts/" . $fontfiles[mt_rand(0,count($fontfiles)-1)]得到的结果无效。你新文件中有没建个文件夹放相对应字体的文件"arial.ttf","ariblk.ttf","simkai.ttf","SIMLI.TTF","simsun.ttc","STLITI.TTF"这些
打开App,查看更多内容
随时随地看视频慕课网APP