我有一个CAPTCHA类,它具有生成CAPTCHA的功能。正如您知道何时生成CAPTCHA一样,它应该像图像一样返回,以便页面将Content-type更改为image mime-type。 CAPTCHA类本身工作得很好,但我希望将其类包含在另一个类中,并通过调用相对函数来处理自己生成的CAPTCHA。我知道在我的代码中有一些错误,但我无法弄清楚,只要我从CAPTCHA生成器的控制器的替代类调用函数,PHP弹出一个错误,它说'生成的图像包含错误并不能显示'。我知道这是因为标题改变,但我想找到一种方法来解决这个问题。
有人能告诉我如何实现这个?
我的CAPTCHA课程:
class CaptchaSecurityImages {
public function MakeCaptcha($width='120', $height='40', $session='CaptchaCode', $characters='6') {
$code = $this->generateCode($characters);
/* font size will be 75% of the image height */
$font_size = $height * 0.85;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
/* set the colours */
$background_color = imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 100, 120, 180);
/* generate random dots in background */
for( $i=0; $i
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
}
/* generate random lines in background */
for( $i=0; $i
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
/* create textbox and add text */
$textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
/* output captcha image to browser */
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION[$session] = $code;
}
}我的CAPTCHA控制器:
class CaptchaController
{
/// Constructor
public function __construct() { }
/// Generate captcha
public function Generate($width = '120', $height = '40', $session = 'CaptchaCode', $chars = '6')
{
include('captcha.php');
$captcha = new CaptchaSecurityImages();
$captcha->MakeCaptcha();
}
}