自用captcha验证码类
1 <?php 2 //header("Content-type: image/GIF"); 3 class Captcha{ 4 private $width; 5 private $height; 6 private $counts;//要生成的验证码个数 7 private $distrubCode;//用于生成验证码的字符串 8 private $fontUrl;//字体 9 10 public function __construct($arr = array()){ 11 $this->width = isset($arr['width']) ? $arr['width'] : 120; 12 $this->height = isset($arr['height']) ? $arr['height'] : 30; 13 $this->counts = isset($arr['counts']) ? $arr['counts'] : 5; 14 $this->distrubCode = isset($arr['distrubCode']) ? $arr['distrubCode'] : '1235467890qwertyuipkjhgfdaszxcvbnm'; 15 $this->fontUrl = isset($arr['fontUrl']) ? $arr['fontUrl'] : 'ITCBLKAD.TTF'; 16 //session_start(); 17 $this->getCaptchar(); 18 } 19 //输出验证码 20 public function getCaptchar(){ 21 //创建图片在内存 22 $image = $this->createImageSource(); 23 //设置图片背景色 24 $this->setImageBGColor($image); 25 //设置验证码 26 $this->setCode($image); 27 //字符干扰码 28 $this->setDistrubCode($image); 29 //黑点干扰码 30 //$this->setDistrubPixel($image); 31 //干扰线 32 //$this->setDistrubLine($image); 33 //雪花干扰 34 //$this->setDistrubString($image); 35 //生成圆孤干扰 36 //$this->setDistrubArc($image); 37 //生成虚线干扰 38 //$this->setDistrubImaginary($image); 39 //输出图片 40 imagegif($image); 41 //从内存中销毁 42 imagedestroy($image); 43 } 44 //创建画布 45 private function createImageSource(){ 46 return imagecreatetruecolor($this->width,$this->height); 47 } 48 //设置画布的背景色 49 private function setImageBGColor($image){ 50 //创建画布的背景色 51 $bgcolor = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)); 52 //为画布填充背景色 53 imagefill($image,0,0,$bgcolor); 54 } 55 //设置验证码中的干扰码 56 private function setDistrubCode($image){ 57 $count_h = $this->height; 58 //设置干扰码的数量 59 $cou = floor($count_h/2); 60 for($i = 0;$i<$cou;$i++){ 61 //随机生成X,Y 62 $x = mt_rand(0,$this->width); 63 $y = mt_rand(0,$this->height); 64 //生成倾斜的角度 65 $angle = mt_rand(0,360); 66 //生成字体的大小 67 $fontSize = mt_rand(8,15); 68 //字体的样式 69 $fontUrl = $this->fontUrl; 70 //用于生成验证码的字符串 71 $distrubCode = $this->distrubCode; 72 //取得字符串的长度 73 $codeLenth = strlen($distrubCode); 74 //随机取出一个字符 75 $dscode = $distrubCode[mt_rand(0,$codeLenth-1)]; 76 //创建的一个字体颜色 77 $fontColor = imagecolorallocate($image,mt_rand(40,140),mt_rand(40,140),mt_rand(40,140)); 78 //把字体添加到画布上 79 imagettftext($image,$fontSize,$angle,$x,$y,$fontColor,$fontUrl,$dscode); 80 } 81 } 82 //生成验证码 83 private function setCode($image){ 84 $width = $this->width; 85 $height = $this->height; 86 $counts = $this->counts; 87 $code = $this->sessionCode(); 88 //生成Y坐标 89 $y = floor($height/2)+floor($height/4)+8; 90 $fontSize = mt_rand(30,35); 91 //$fontUrl = 'C:\Windows\Fonts\ALGER.TTF'; 92 $fontUrl = 'ALGER.TTF'; 93 //$fontUrl = $this->fontUrl; 94 //添加验证码 95 $counts=$this->counts; 96 for($i=0;$i<$counts;$i++){ 97 //取得字符 98 $char = $code[$i]; 99 //生成X的坐标 100 $x = floor($width/$counts-1)*$i+3; 101 //倾斜角度 102 $angle = mt_rand(-20,30); 103 $fontColor = imagecolorallocate($image,mt_rand(0,50),mt_rand(50,100),mt_rand(50,140)); 104 //把字体添加到画布上 105 imagettftext($image,$fontSize,$angle,$x,$y,$fontColor,$fontUrl,$char); 106 } 107 } 108 //生成要进行验证的字符串 109 public function sessionCode(){ 110 $code = $this->distrubCode; 111 $len = strlen($code); 112 $_dscode = ''; 113 $counts = $this->counts; 114 //取得要验证的字符串 115 for($i = 0;$i<$counts;$i++){ 116 $dscode = $code[mt_rand(0,$len-1)]; 117 $_dscode .= $dscode; 118 } 119 $_SESSION['captcha'] = $_dscode; 120 return $_dscode; 121 } 122 //生成黑点干扰码 123 private function setDistrubPixel($image){ 124 $width = $this->width; 125 $height = $this->height; 126 //生成黑点 127 for($i=0;$i<$width*5;$i++){ 128 $pixelColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); 129 imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),$pixelColor); 130 } 131 } 132 //生成干扰线 133 private function setDistrubLine($image){ 134 $width = $this->width; 135 $height = $this->height; 136 //生成干扰线 137 for($i=0;$i<30;$i++){ 138 $lineColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); 139 imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$lineColor); 140 } 141 } 142 //生成雪花干扰 143 private function setDistrubString($image){ 144 $width = $this->width; 145 $height = $this->height; 146 //生成随机雪花 147 for($i=0;$i<50;$i++){ 148 $stringColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); 149 imagestring($image,mt_rand(1,5),mt_rand(0,$width),mt_rand(0,$height),'*',$stringColor); 150 } 151 } 152 //生成圆弧干扰 153 private function setDistrubArc($image){ 154 $width = $this->width; 155 $height = $this->height; 156 //生成随机圆弧 157 for($i=0;$i<30;$i++){ 158 $arcColor = imagecolorallocate($image,mt_rand(100,200),mt_rand(100,200),mt_rand(100,200)); 159 imagearc($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width/2),mt_rand(0,$height/2),mt_rand(0,360),mt_rand(100,360),$arcColor); 160 } 161 } 162 //生成虚线干扰 163 private function setDistrubImaginary($image){ 164 $width = $this->width; 165 $height = $this->height; 166 //生成虚线干扰线 167 for($i=0;$i<20;$i++){ 168 $black = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 169 $gray = imagecolorallocate($image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 170 $style = array ($black,$black,$black,$black,$black,$gray,$gray,$gray,$gray,$gray); 171 imagesetstyle($image,$style); 172 imageline($image,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),IMG_COLOR_STYLED); 173 } 174 } 175 public static function checkCaptcha($char){ 176 return (strtolower($char) === strtolower($_SESSION['captcha'])); 177 } 178 }