示例#1
0
    public function createCaptcha()
    {
        $captcha = '';
        $symbol = '0';
        $width = 420;
        $height = 70;
        $font = 'fonts/bellb.ttf';
        $fontsize = 20;
        $captchaLength = rand(1, 1);
        $im = imagecreatetruecolor($width, $height);
        $bg = imagecolorallocatealpha($im, 0, 0, 0, 127);
        imagefill($im, 0, 0, $bg);
        for ($i = 0; $i < $captchaLength; $i++) {
            $captcha .= $symbol[rand(0, strlen($symbol) - 1)];
            $x = ($width - 20) / $captchaLength * $i + 10;
            $x = rand($x, $x + 4);
            $y = $height - ($height - $fontsize) / 2;
            $curcolor = imagecolorallocate($im, rand(0, 100), rand(0, 100), rand(0, 100));
            $angle = rand(-25, 25);
            imagettftext($im, $fontsize, $angle, $x, $y, $curcolor, $font, $captcha[$i]);
        }
        session_start();
        $_SESSION['captcha'] = $captcha;
        header('Content-type: image/png');
        imagepng($im);
        imagedestroy($im);
    }
}
$obj = new captcha();
$obj->createCaptcha();
<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
define("captchaCode", "");
include 'captcha.class.php';
if (isset($_GET['aux'])) {
    $aux = $_GET['aux'];
} else {
    $aux = "0";
}
if (isset($_GET['input_captcha'])) {
    $input_captcha = $_GET['input_captcha'];
} else {
    $input_captcha = "0";
}
if ($aux == 'createCaptcha') {
    $captcha = new captcha("fonts/");
    $htmlCaptcha = $captcha->createCaptcha();
    echo $htmlCaptcha;
} else {
    if ($aux == 'validateCaptcha') {
        if ($input_captcha != "0") {
            $captcha = new captcha();
            $validCaptcha = $captcha->validateCaptcha($input_captcha);
            echo $validCaptcha;
        }
    }
}