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; } /** * 指定字型檔所在路徑,預設為當前檔案夾下arial.ttf檔案 * @param $fontfile 檔案路徑 * @return void */ function loadfont($fontfile) { $this->fontfile = $fontfile; } /** * 圖片輸出方法,在執行本方法前程式不應該有任何形式的輸出 * @return void; */ function stroke() { $this->savecode(); self::sendheader(); imagegif( $this->validate ); imagedestroy( $this->validate ); imagedestroy( $this->image ); } /** * 圖片儲存方法 * @param $filename 儲存路徑 * @return void */ function save($filename) { $this->savecode(); imagegif( $this->validate , $filename ); imagedestroy( $this->validate ); imagedestroy( $this->image ); } /** * 驗證碼驗證方法 * @param $input 要驗證的字串,即使用者的輸入內容 * @return boolean 驗證結果 */ function verify($input) { $input=strtolower($input); $targetcode=$this->authcode($input); $code=$this->getcookie(); if (empty($code)||$code!=$targetcode) $result= false; else $result=true; $_cookie[$this->codecookiename]=''; setcookie ( $this->codecookiename, '', - 1 ); return $result; } /** * 圖片建立方法 * @return void; */ 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( '.' ) ); // name the font to be used (note the lack of the .ttf extension 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 ); } |