cls.ValidateNumber.php

<? php


class ImgCode

{


public $BackGround = false ;



public $BackColor = ' #FFFFFF ' ;



public $OnlyKey = false ;

public $OnlyKeyWay = 0 ;

public $OnlyKeySeed = ' MD5 ' ;



public $KeyOutPutEncrypt = false ;



public $OutPutWay = 0 ;



public $SID = '' ;

public $OutPutText = '' ;



public $arlFont = array ( ' 1.ttf ' , ' 2.ttf ' , ' 3.ttf ' , ' 4.ttf ' );



private $Image = '' ;



public $NoisePosition = false ;



public $FontSize = 0 ;

public $BuildLen = 4 ;



public $ImgWidth = 100 ;

public $ImgHeight = 30 ;



public $DotNoise = false ;

public $DotNoiseWay = ' auto ' ;

public $DotNoiseCol_r = array ( 100 , 255 );

public $DotNoiseCol_b = array ( 100 , 255 );

public $DotNoiseCol_g = array ( 100 , 255 );

public $DotNoisePix = 300 ;



public $LineNoise = false ;

public $LineNoiseWay = ' auto ' ;

public $LineNoiseCol_r = array ( 0 , 255 );

public $LineNoiseCol_b = array ( 0 , 255 );

public $LineNoiseCol_g = array ( 0 , 255 );

public $LineNoisePix = 300 ;



public $Border = false ;

public $BorderWay = ' auto ' ;

public $BorderCol_r = array ( 0 , 150 );

public $BorderCol_b = array ( 0 , 150 );

public $BorderCol_g = array ( 0 , 150 );




public $RandTextWay = ' auto ' ;

public $RandTextCol_r = array ( 0 , 150 );

public $RandTextCol_b = array ( 0 , 150 );

public $RandTextCol_g = array ( 0 , 150 );



public $FontSizeFloat = 10 ;



public $FontBE = 15 ;



public $FontBX = 5 ;



public $FontLean = 18 ;



public $Toothful = 2 ;



public $VerilyTine = true ;



public $Gather = 0 ;

public $GatherTeam = array (

0 => array ( ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' , ' G ' , ' H ' , ' I ' , ' J ' , ' K ' , ' L ' , ' M ' , ' N ' , ' O ' , ' P ' , ' Q ' , ' R ' , ' S ' , ' T ' , ' U ' , ' V ' , ' W ' , ' X ' , ' Y ' , ' Z ' ) ,

1 => array ( ' a ' , ' b ' , ' c ' , ' d ' , ' e ' , ' f ' , ' g ' , ' h ' , ' i ' , ' j ' , ' j ' , ' l ' , ' m ' , ' n ' , ' o ' , ' p ' , ' q ' , ' r ' , ' s ' , ' t ' , ' u ' , ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ) ,

2 => array ( ' 0 ' , ' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' , ' 6 ' , ' 7 ' , ' 8 ' , ' 9 ' ));



public function ImgCode( $img_w = 100 , $img_h = 30 , $f_size = 0 , $bg = false , $b_len = 4 )

{

if ( ! $bg )

{

$this -> ImgWidth = $img_w ; // 寬度指派

$this -> ImgHeight = $img_h ; // 高度指派

} else {

if ( ! file_exists ( $bg )) die ( ' Find not BackGround File 1 ' );

$ImgSize = GetImageSize ( $bg );

$this -> ImgWidth = $ImgSize [ 0 ]; // 寬度指派

$this -> ImgHeight = $ImgSize [ 1 ]; // 高度指派

}


$this -> BuildLen = $b_len ; // 驗證碼長度指派



if ( $f_size !== 0 )

{

$this -> FontSize = $f_size ;

} else {

$this -> FontSize = $img_w / $b_len ;

}



session_start ();

@ session_unregister ( " SESSION_KEY " ); // 若之前已存在一個 SESSION 會話,将其關閉

@ session_unregister ( " ENCRYPT_KEY " ); //


session_register ( " ENCRYPT_KEY " );

session_register ( " SESSION_KEY " ); // 注冊 SESSION_KEY


$this -> SID = session_id (); // 給予 SESSIONID

if ( ! session_is_registered ( " SESSION_KEY " ))

{

die ( " session registered lost " );

}

}



public function Build()

{


$m_Cade = '' ;



$this -> FontSize = $this -> FontSize - $this -> Toothful;



if ( $this -> BackGround)

{

if ( ! file_exists ( $this -> BackGround)) die ( ' Find not BackGround File 2 ' );

$ImgSize = GetImageSize ( $this -> BackGround);

$this -> ImgWidth = $ImgSize [ 0 ]; // 寬度指派

$this -> ImgHeight = $ImgSize [ 1 ]; // 高度指派


switch ( $ImgSize [ 2 ])

{

case 1 :

$this -> Image = @imagecreatefromgif( $this -> BackGround);

break ;

case 2 :

$this -> Image = @imagecreatefromjpeg( $this -> BackGround);

break ;

case 3 :

$this -> Image = @imagecreatefrompng( $this -> BackGround);

break ;

}

} else {

if ( ! $VerilyTine )

{

$this -> Image = @imagecreate( $this -> ImgWidth , $this -> ImgHeight);

} else {

$this -> Image = @imagecreatetruecolor( $this -> ImgWidth , $this -> ImgHeight);

}

}


if ( ! $this -> Image) die ( " Cannot Initialize new GD image stream " ); // 是否建立成功

if ( ! $this -> BackGround)

{

$FunBackColor = $this -> FetchColor( $this -> BackColor);

imageFilledRectangle( $this -> Image , 0 , 0 , $this -> ImgWidth , $this -> ImgHeight , $FunBackColor ); // 建立一個矩型并填充

}


// 如果字型超出高度則重設字型大小等于高度 - 2 , 防止超出圖形區

if ( $this -> FontSize > $this -> ImgHeight) $this -> FontSize = $this -> ImgHeight;


// 設定字型生成的左邊界

$FunLeft = ( $this -> ImgWidth - $this -> BuildLen * ( $this -> FontSize + $this -> FontSize / 10 )) / $this -> FontSize;


// 幹擾資訊出現在底部的判斷

if ( $this -> NoisePosition == false ) $this -> setNoise();


for ( $i = 0 ; $i < $this -> BuildLen; $i ++ )

{

if ( ! $this -> OnlyKey)

{

$UseKey = array_reverse ( $this -> GatherTeam[ $this -> Gather]); // 如果為内定内容資料集,确定使用内容資料集,并反轉數組

$RandText = $UseKey [ mt_rand ( 0 , count ( $UseKey ) - 1 )]; // 擾亂計算本循環的字元

$m_Cade .= $RandText ; // 儲存驗證碼資訊

} else {

// 是否區分大小寫生成唯一驗證碼序列


if ( $this -> OnlyKeyWay == 0 )

{

if ( $this -> OnlyKeySeed == ' MD5 ' ) // 加密方式判斷

{

$Rand = str_split ( strtolower ( md5 ( microtime ())));

} else {

$Rand = str_split ( strtolower ( sha1 ( microtime ())));

}

} else {

if ( $this -> OnlyKeySeed == ' MD5 ' ) // 加密方式判斷

{

$Rand = str_split ( strtoupper ( md5 ( microtime ())));

} else {

$Rand = str_split ( strtoupper ( sha1 ( microtime ())));

}

}


$Rand = array_reverse ( $Rand );

$RandText = $Rand [ mt_rand ( 0 , count ( $Rand ) - 1 )];

$m_Cade .= $RandText ; // 儲存驗證碼資訊

}


// 設定字型顔色

if ( $this -> RandTextWay == ' auto ' )

{

$FunFontColor = imageColorAllocate( $this -> Image , mt_rand ( $this -> RandTextCol_r[ 0 ] , $this -> RandTextCol_r[ 1 ]) ,

mt_rand ( $this -> RandTextCol_b[ 0 ] , $this -> RandTextCol_b[ 1 ]) ,

mt_rand ( $this -> RandTextCol_g[ 0 ] , $this -> RandTextCol_g[ 1 ]));

} else {

$FunFontColor = $this -> FetchColor( $this -> RandTextWay);

}



// 設定随機字型資訊

$FontType = $this -> arlFont[ mt_rand ( 0 , count ( $this -> arlFont) - 1 )];


// 設定循環每個字元時的不同大小

$RandSize = mt_rand ( $this -> FontSize - $this -> FontSize / $this -> FontSizeFloat , // 下限

$this -> FontSize + $this -> FontSize / $this -> FontSizeFloat); // 上限


// 設定字型基本點 x (左右控制)

$Location_x = $FunLeft + ( $i * $this -> FontSize + $this -> FontSize / $this -> FontBE) + $this -> FontBX;


// 設定字型基本點 y (上下控制)

$Location_y = mt_rand ( $this -> FontSize - $this -> FontSize / $this -> FontBE ,

$this -> FontSize + $this -> FontSize / $this -> FontBE);


imagettftext( $this -> Image , $RandSize , mt_rand ( - $this -> FontLean , $this -> FontLean) , $Location_x , $Location_y ,

$FunFontColor , $FontType , iconv ( ' gb2312 ' , ' UTF-8 ' , $RandText ));

}


imageantialias( $this -> Image , true );


$_SESSION [ " SESSION_ID " ] = $this -> SID;


if ( $this -> OutPutWay == 0 )

{

$_SESSION [ " SESSION_KEY " ] = $m_Cade ;

$this -> OutPutText = $m_Cade ;

} else if ( $this -> OutPutWay == 1 ) {

$EnText = sha1 ( strrev ( md5 ( $m_Cade )));

$_SESSION [ " ENCRYPT_KEY " ] = $EnText ;

$this -> OutPutText = sha1 ( strrev ( md5 ( $m_Cade )));

} else {

$EnText = sha1 ( strrev ( md5 ( $m_Cade )));

$_SESSION [ " ENCRYPT_KEY " ] = $EnText ;

$_SESSION [ " SESSION_KEY " ] = $m_Cade ;

$this -> OutPutText = array ( 0 => $m_Cade , 1 => $EnText );

}


// 幹擾資訊出現在第一層的判斷

if ( $this -> NoisePosition == true ) $this -> setNoise();


header ( " Content-type: image/png " );

imagePng( $this -> Image);

imagedestroy( $this -> Image);

}


private function setNoise()

{

// 安排邊框

$this -> setBorser();

// 安排點性幹擾

$this -> setDotNoise();

// 安排線性幹擾

$this -> setLineNoise();

}



private function setBorser()

{

if ( $this -> Border)

{

if ( $this -> BorderWay == ' auto ' )

{

$BorderColor = imageColorAllocate( $this -> Image , mt_rand ( $this -> BorderCol_r[ 0 ] , $this -> BorderCol_r[ 1 ]) ,

mt_rand ( $this -> BorderCol_b[ 0 ] , $this -> BorderCol_b[ 1 ]) ,

mt_rand ( $this -> BorderCol_g[ 0 ] , $this -> BorderCol_g[ 1 ]));

imageRectangle( $this -> Image , 0 , 0 , $this -> ImgWidth - 1 , $this -> ImgHeight - 1 , $BorderColor );

} else {

$BorderColor = $this -> FetchColor( $this -> BorderWay);

imageRectangle( $this -> Image , 0 , 0 , $this -> ImgWidth - 1 , $this -> ImgHeight - 1 , $BorderColor );

}

return true ;

} else {

return false ;

}

}




private function setDotNoise()

{

if ( $this -> DotNoise)

{

if ( $this -> DotNoiseWay == ' auto ' )

{

for ( $i = 0 ; $i < $this -> DotNoisePix; $i ++ ){

$randColor = imageColorAllocate( $this -> Image , mt_rand ( $this -> DotNoiseCol_r[ 0 ] , $this -> DotNoiseCol_r[ 1 ]) ,

mt_rand ( $this -> DotNoiseCol_b[ 0 ] , $this -> DotNoiseCol_b[ 1 ]) ,

mt_rand ( $this -> DotNoiseCol_g[ 0 ] , $this -> DotNoiseCol_g[ 1 ]));

imageSetPixel( $this -> Image , mt_rand ( 0 , $this -> ImgWidth) , mt_rand ( 0 , $this -> ImgHeight) , $randColor );

}

} else {

$DotColor = $this -> FetchColor( $this -> DotNoiseWay);

for ( $i = 0 ; $i < $this -> DotNoisePix; $i ++ ){

imageSetPixel( $this -> Image , mt_rand ( 0 , $this -> ImgWidth) , mt_rand ( 0 , $this -> ImgHeight) , $DotColor );

}

}

return true ;

} else {

return false ;

}

}



private function setLineNoise()

{

if ( $this -> LineNoise)

{

if ( $this -> LineNoiseWay == ' auto ' )

{

for ( $i = 0 ; $i < $this -> LineNoisePix; $i ++ )

{

$LineColor = imageColorAllocate( $this -> Image , mt_rand ( $this -> LineNoiseCol_r[ 0 ] , $this -> LineNoiseCol_r[ 1 ]) ,

mt_rand ( $this -> LineNoiseCol_b[ 0 ] , $this -> LineNoiseCol_b[ 1 ]) ,

mt_rand ( $this -> LineNoiseCol_g[ 0 ] , $this -> LineNoiseCol_g[ 1 ]));

imageline( $this -> Image , mt_rand ( 0 , $this -> ImgWidth) , mt_rand ( 0 , $this -> ImgHeight) ,

mt_rand ( 0 , $this -> ImgWidth) , mt_rand ( 0 , $this -> ImgHeight) , $LineColor );

}

} else {

$LineColor = $this -> FetchColor( $this -> LineNoiseWay);

for ( $i = 0 ; $i < $this -> LineNoisePix; $i ++ )

{

imageline( $this -> Image , mt_rand ( 0 , $this -> ImgWidth) , mt_rand ( 0 , $this -> ImgHeight) ,

mt_rand ( 0 , $this -> ImgWidth) , mt_rand ( 0 , $this -> ImgHeight) , $LineColor );

}

}

}

}



private function FetchColor( $Color = '' )

{

$Color = ereg_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 ( $this -> Image , $r , $b , $g );

return $Color ;

}

}

?>
必須設定好,所需要的字型檔案及其他資源。