验证码没有显示

时间:2014-08-25 13:16:16

标签: php html captcha

好的......所以......我正在尝试创建我自己的Captcha,以便在我工作的网页上使用。 由于我在php上非常注重菜单,因此我遵循了youtube指南

我在这个视频中遵循了每一个步骤,他所做的一切我都做了,我下载了相同的图像lybrary(AnonymousClippings.tff btw)等等但是由于某种原因我的验证码图像没有显示。

我需要文件,一个用于验证码,一个用于索引(调用图像功能以便我可以看到图像)并在表单中使用它。

我确定不显示图像的原因非常愚蠢,但我无法确定问题所在。

如果你知道一种更简单的方法来创建我自己的验证码,我会感到难以接受。

captcha.php

<?php

class captcha{
private static $captcha = "__captcha__";
private static $font;
private static $width = 70;
private static $height = 70;
private static $font_size = 40;
private static $character_width = 40;

private static function session_start(){
    if(!isset($_SESSION)){
        session_start();
    }
}
private static function set_font(){
self::$font = self::$captcha;
$AnonymousClippings = "HERE GOES ALL POSSIBLE CAHRS THAT THE CAPTCHA WILL BE ABLE TO SHOW FROM AnonymousClippings LYBRARY";
$handle = fopen(self::$font,"w+");
fwrite($handle, base64_encode($AnonymousClippings));
fclose($handle);

return self::$font;
}
public static function get_random(){
$type = rand(0,2);

switch ($type){
    case 2:
        $random = chr(rand(65, 90));
        break;
    case 1:
        $random = chr(rand(97, 122));
        break;
    default:
        $random = chr(rand(0,9));
        break;

}
return $random;

}

private static function get_width(){
return self::$width;

}

private static function get_height(){
return self::$height;
}

private static function generate_code($length){
self::session_start();

$code=null;

for($i=0;$i<$length;$i++){
    $code.=self::get_random();
}
$_SESSION[self::$captcha] = $code;

self::$width = $length * self::$character_width;

return $code;

}

public static function image(){

$length = 6;
$code = self::generate_code($length);

self::set_font();
ob_start();

$image = imagecreatetruecolor(self::get_width(),self::get_height());
$white = imagecolorallocate($image, 255, 255, 255);

imagefilledrectangle($image, 0, 0, self::get_width(), self::get_height(), $white);

for($dot=0;$dot<2000;$dot++){
    $red = rand(0,255);
    $green = rand(0,255);
    $blue = rand(0,255);

    $dot_color = imagecolorallocate($image, $red, $green, $blue);
            $x1 = rand(0,self::get_width());
            $y1= rand(0,self::get_height());
            $x2 = $x1 +1;
            $y2 = $y1 +1;
            imageline($image, $x1, $y1, $x2, $y2, $dot_color);

}

for($start = $length*(-1); $start<0; $start++){
    $color = imagecolorallocate($image, rand(0,177), rand(0,177), rand(0,177));
    $character = substr($code, $start,1);

    $x =($start+6)*self::$character_width;
    $y = rand(self::get_height()-20, self::get_height() -10);

    imagettftext($image, self::$font_size, 0, $x, $y, $color, self::$font, $character);
}
imagepng($image);
imagedestroy($image);

$source = ob_get_contents();
ob_end_clean();

unlink(self::$font);

return "data:image/png;base64,".base64_encode($source);

}

public static function get_code(){
self::session_start();
return $_SESSION[self::$captcha];

}



}


?>

index1.php

<?php

require_once("captcha.php");


if(isset($_POST["code"])){
    if($_POST["code"]== captcha::get_code()){
        echo "good";
    }else{
        echo "wrong";
    }
}


?>


<form method="post">
<img src=" <?php captcha::image() ?>"/><br>
<input type="text" name="code"/><br>
<input type="submit" value="Check"/>
</form>

1 个答案:

答案 0 :(得分:2)

这些方法最终会返回数据,但您永远不会回显输出。

return "data:image/png;base64,".base64_encode($source);

部分解决:

<img src="<?php echo captcha::image(); ?>"/><br>

从评论中获取,下一个错误是字体路径:

self::$font = self::$captcha;

不要将em设置为相同的值。 $font必须指向字体路径(例如./arial.ttf)。