dougao2830 2014-08-25 13:16
浏览 149

验证码没有显示

Ok...so...i'm trying the create my own Captcha to use on a webpage i'm working on. Since i'm pretty much a noob on php i followed a youtube guide

i followed EVERY SINGLE STEP in this video, everything he did i did too, i dowloaded the same image lybrary(AnonymousClippings.tff btw) and etc BUT for some reason my captcha image is not showing.

I have to files, one for the captcha and one for the index(to call the image function so i can see the image) and use it in a form.

i'm sure the reason for not showing the image is pretty stupid but i just can't figure what's the problem.

if you know a easier way to create my own captcha i would apreciate.

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条回答 默认 最新

  • duankuai6991 2014-08-25 13:27
    关注

    The methods end up returning the data but you never echo the output.

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

    Partially solved:

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

    Taken from the comments, next error is font path:

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

    Don't set em to the same value. $font must point to a font path (like ./arial.ttf).

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题