php生成验证码类代码 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
- class securecode
- {
- private static $instance=null;
- private $code = '';
- private $fontfile;
- private $validate;
- private $image;
- private $specialadd = 'special string for securecode';
- private $codeexpire=86400;
- private $codecookiename='secure_code';
-
-
-
-
- private function securecode()
- {
- $this->fontfile = dirname( __file__ ) . '/arial.ttf';
- }
-
- private function __construct()
- {
- $this->securecode();
- }
-
- public static function getinstance()
- {
- if (self::$instance==null)
- self::$instance=new self();
-
- return self::$instance;
- }
-
-
-
-
-
-
- function loadfont($fontfile)
- {
- $this->fontfile = $fontfile;
- }
-
-
-
-
-
- function stroke()
- {
- $this->savecode();
- self::sendheader();
- imagegif( $this->validate );
- imagedestroy( $this->validate );
- imagedestroy( $this->image );
- }
-
-
-
-
-
-
- function save($filename)
- {
- $this->savecode();
- imagegif( $this->validate , $filename );
- imagedestroy( $this->validate );
- imagedestroy( $this->image );
- }
-
-
-
-
-
-
- function verify($input)
- {
- $input=strtolower($input);
- $targetcode=$this->authcode($input);
- $code=$this->getcookie();
- if (emptyempty($code)||$code!=$targetcode)
- $result= false;
- else
- $result=true;
- $_cookie[$this->codecookiename]='';
- setcookie ( $this->codecookiename, '', - 1 );
- return $result;
- }
-
-
-
-
-
- function createimage()
- {
- $this->randcode();
-
- $size = 30;
- $width = 90;
- $height = 35;
- $degrees = array (
- rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
- );
-
-
- for ($i = 0; $i < 4; ++$i)
- {
- if (rand() % 2);
- else $degrees[$i] = -$degrees[$i];
- }
-
- $this->image = imagecreatetruecolor( $size , $size );
- $this->validate = imagecreatetruecolor( $width , $height );
- $back = imagecolorallocate( $this->image , 255 , 255 , 255 );
- $border = imagecolorallocate( $this->image , 0 , 0 , 0 );
- imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
-
- for ($i = 0; $i < 4; ++$i)
- {
- $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
-
- if ($temp[2] > 60) $temp[2] = 60;
-
- $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
- $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
- }
-
- for ($i = 0; $i < 200; ++$i)
- {
- $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
- imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
- }
-
- $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
-
- if ($temp[2] < 200) $temp[2] = 255;
-
- $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
- $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
-
- self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
-
- imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
- putenv( 'gdfontpath=' . realpath( '.' ) );
-
-
- imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
-
- $this->image = imagerotate( $this->image , $degrees[0] , $back );
- imagecolortransparent( $this->image , $back );
- imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
-
- $this->image = imagecreatetruecolor( $size , $size );
- imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
- imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
- $this->image = imagerotate( $this->image , $degrees[1] , $back );
- imagecolortransparent( $this->image , $back );
- imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
-
- $this->image = imagecreatetruecolor( $size , $size );
- imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
- imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
- $this->image = imagerotate( $this->image , $degrees[2] , $back );
- imagecolortransparent( $this->image , $back );
- imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
-
- $this->image = imagecreatetruecolor( $size , $size );
- imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
- imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
- $this->image = imagerotate( $this->image , $degrees[3] , $back );
- imagecolortransparent( $this->image , $back );
- imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
- imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
- }
-
-
-
-
-
- function getcode()
- {
- return $this->code;
- }
-
-
-
-
-
- protected function randcode()
- {
- $alphastr = 'abcdefghijklmnpqrstuvwxyz123456789';
- $randstr = array (
- $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}
- );
- $this->code = strtolower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] );
- }
-
-
-
-
-
-
-
-
- protected static function rgbtohsv($r, $g, $b)
- {
- $tmp = min( $r , $g );
- $min = min( $tmp , $b );
- $tmp = max( $r , $g );
- $max = max( $tmp , $b );
- $v = $max;
- $delta = $max - $min;
-
- if ($max != 0) $s = $delta / $max;
- else
- {
- $s = 0;
-
- return;
- }
- if ($r == $max) $h = ($g - $b) / $delta;
- else if ($g == $max) $h = 2 + ($b - $r) / $delta;
- else $h = 4 + ($r - $g) / $delta;
-
-
-
- $h *= 60;
- if ($h < 0) $h += 360;
- return array (
- $h, $s, $v
- );
- }
-
-
-
-
-
-
-
-
- protected static function hsvtorgb($h, $s, $v)
- {
- if ($s == 0)
- {
-
- $r = $g = $b = $v;
- return;
- }
-
- $h /= 60;
- $i = floor( $h );
- $f = $h - $i;
- $p = $v * (1 - $s);
- $q = $v * (1 - $s * $f);
- $t = $v * (1 - $s * (1 - $f));
-
- switch ($i)
- {
- case 0 :
- $r = $v;
- $g = $t;
- $b = $p;
- break;
- case 1 :
- $r = $q;
- $g = $v;
- $b = $p;
- break;
- case 2 :
- $r = $p;
- $g = $v;
- $b = $t;
- break;
- case 3 :
- $r = $p;
- $g = $q;
- $b = $v;
- break;
- case 4 :
- $r = $t;
- $g = $p;
- $b = $v;
- break;
- default :
- $r = $v;
- $g = $p;
- $b = $q;
- break;
- }
- return array (
- $r, $g, $b
- );
- }
-
-
-
-
-
- protected function savecode()
- {
- $code = $this->authcode($this->code);
- $this->setcookie($code);
- }
-
-
-
-
-
- protected function getcookie()
- {
- if (emptyempty( $_cookie[$this->codecookiename] ))
- {
- return '';
- }
- else
- {
- return addslashes($_cookie[$this->codecookiename]);
- }
- }
-
-
-
-
-
-
- protected function setcookie($code)
- {
- $expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0;
- setcookie( $this->codecookiename , $code, $expire );
- }
-
-
-
-
-
-
- protected function authcode($code)
- {
- return md5($code.$this->specialadd);
- }
-
-
-
-
-
-
- protected static function imagelinethick($image, $color)
- {
- $k = rand( 5 , 20 );
- for ($px = 0; $px < 400; $px = $px + 1)
- {
- $y = $k * sin( 0.1 * ($px) );
- for ($i = 0; $i < 2; $i++)
- {
- imagesetpixel( $image , $px , $y + 10 + $i , $color );
- }
-
- }
- }
-
-
-
-
-
- protected static function sendheader()
- {
- header( "pragma: no-cache" );
- header( "cache-control: max-age=1, s-maxage=1, no-cache, must-revalidate" );
- header( 'content-type: image/gif' );
- }
- }
|
|
|
|