当前位置:首页> PHP教程> php进阶
关键字:
文章内容
图形认证
 
 
修改时间:[2011/10/13 13:42]    阅读次数:[1960]    发表者:[起缘]
 
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004                                                   |
// | Beijing Innovative Linkage Tech Ltd.                                 |
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Authors: 佟杰义<[email protected]>                                    |
// +----------------------------------------------------------------------+
//
// $Id: vImage.inc.php
//

/**
*
* @version  v 1.0
* @author   佟杰义<[email protected]>
* @access   Public
*/


/**
 * 图形的验证码的类(显示,传递验证码)
 */

class validateImage
{

    var 
$x;
    var 
$y;
    var 
$numChars;
    var 
$Code;
    var 
$Width;
    var 
$Height;
    var 
$BG;
    var 
$colTxt;
    var 
$colBorder;
    var 
$numCirculos;

    function 
validateImage()
    {
        
$this->x = $x;
        
$this->y = $y = "6";
        
$this->numChars = $numChars = "4";
        
$this->Code = $Code;
        
$this->Width = $Width = "80";
        
$this->Height = $Height = "25";
        
$this->BG = $BG = "255 255 255";
        
$this->colTxt = $colTxt = "0 0 0 0";
        
$this->Border = $colBorder = "100 100 100";
        
$this->numCirculos = $numCirculos = "800";
    }

    
//Create base Image
    
function createImage()
    {
        
//Create a image
        
$im = imagecreate ($this->Width, $this->Height) or die ("Cannot Initialize new GD image stream");

        
//Get the RGB color code
        
$colorBG = explode(" ", $this->BG);

        
$colorBorder = explode(" ", $this->Border);

        
$colorTxt = explode(" ", $this->colTxt);

        
//put the background color on the image
        
$imBG = imagecolorallocate ($im, $colorBG[0], $colorBG[1], $colorBG[2]);

        
//put the border on the image
        
$Border = ImageColorAllocate($im, $colorBorder[0], $colorBorder[1], $colorBorder[2]);
        
$imBorder = ImageRectangle($im, 0, 0, $this->Width-1,$this->Height-1, $Border);

        
//put the code color on the image
        
$imTxt = imagecolorallocate ($im, $colorTxt[0], $colorTxt[1], $colorTxt[2]);

        
//Drop 800 points
        
for($i = 0; $i < $this->numCirculos; $i++)
        {
            
$imPoints = imagesetpixel($im, mt_rand(0,80), mt_rand(0,80), $Border);
        }

        
//put the Code on image
        
for($i = 0; $i < $this->numChars; $i++)
        {
            
//get $x's location
            
$this->x = 21 * $i;

            
//get the code
            
$this->Code.= (mt_rand(0,10));

            
$putCode = substr($this->Code, $i, "1");

            
//put the code;
            
$Code = imagestring($im, 5, $this->x, $this->y, $putCode,$imTxt);

        }

        return 
$im;

    }


    
//Transfer the code to next page
    
function transferCode()
    {
        
//get the code
        
$this->createImage();
        
$vCode = $this->Code;

        return 
$vCode;
    }


    
//display the image
    
function show()
    {
        
header("Content-type:image/png");
        
Imagepng($this->createImage());
        
Imagedestroy($this->createImage());
    }

}

if(
$HTTP_GET_VARS["show"] == "true")
{
    
//use and show it
    
$im = new validateImage;

    
$im->show();
}

?> 


(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)
上面的存为 vImage.inc.php
在同级目录,新建立个文件
<?php

/*
*这里放session类
*/

session_start();
session_register(vCode);


include_once(
"vImage.inc.php");

//use and test it
$im = new validateImage;

$vCode = $im->transferCode();


?>
<
HTML>
<
HEAD>
<
TITLE> test </TITLE>
<
META NAME="Generator" CONTENT="EditPlus">
<
META NAME="Author" CONTENT="">
<
META NAME="Keywords" CONTENT="">
<
META NAME="Description" CONTENT="">
</
HEAD>

<
BODY>

<
img src = "vImage.inc.php?show=true">

</
BODY>
</
HTML> 

(非常全面的一个php技术网站, 有相当丰富的文章和源代码.)