查看完整版本: 經一個數字與字母混合認證碼PHP程序

小豬不笨 2007-6-1 05:17 PM

經一個數字與字母混合認證碼PHP程序

這段代碼很不錯,我一直用

[code]<?php
$codename = "verifycode";
$border   = true;
$noise    = true;
$color['number'] = "#E32A3F";
$color['letter'] = "#113F72";
$color['bg']         = "#CCFF00";
$color['border'] = "#AAB90F";
$noisenum = 9;  
$length          = 4;  
$width          = 60;
$height   = 20;
$x = 8; //
$y = 2; //
//------ end config ---------

$image = imagecreate($width, $height);
$backcolor = getcolor($color['bg']);
imagefilledrectangle($image, 0, 0, $width, $height, $bgcolor);

$textall = array_merge(range('A', 'Z'), range(0,9));
$cW = ($width-1.5*$x)/$length;
for ($i = 0; $i < $length; $i++) {
    $tmptext = rand(0, 35);
    $randtext = $textall["$tmptext"];
    $code .= $randtext;
        $cX = $x + $i*$cW;
        if(is_numeric($randtext)){
                $textColor = getcolor($color['number']);
        }else{
                $textColor = getcolor($color['letter']);
        }
        imagestring($image, 5, $cX, $y, $randtext, $textColor);
}

if ($noise)   setnoise();
if ($border){
        $bordercolor = getcolor($color['border']);
    imagerectangle($image, 0, 0, $width - 1, $height - 1, $bordercolor);
}

session_start();
session_register("{$codename}");
$_SESSION["{$codename}"] = $code;
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);

function getcolor($color) {
    global $image;
    $color = eregi_replace("^#", "", $color);
    $r = $color[0] . $color[1];
    $r = hexdec($r);
    $b = $color[2] . $color[3];
    $b = hexdec($b);
    $g = $color[4] . $color[5];
    $g = hexdec($g);
    $color = imagecolorallocate($image, $r, $b, $g);
    return $color;
}

function setnoise() {
    global $image, $width, $height, $bgcolor, $noisenum;
    for ($i = 0; $i < $noisenum; $i++) {
        $randColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
        imagesetpixel($image, rand(0, $width), rand(0, $height), $randColor);
    }
}
?>[/code]

[[i] 本帖最後由 小豬不笨 於 2007-6-1 05:19 PM 編輯 [/i]]
頁: [1]
查看完整版本: 經一個數字與字母混合認證碼PHP程序