1. //include_once(ROOT.'gb_php/passport.class.php');
    2. //引入生成图片验证码类
    3. $vcode = new Vcode();
    4. $vcode->setLength(4);
    5. //$vcode->setWidth(200);
    6. //$vcode->setHeight(88);
    7. $vcode->setShowBorder(false);
    8. $vcode->setBgColor('#ffffff');
    9. $vcode->setFontColor('#6565FE');
    10. //$vcode->setFontSize(50);
    11. $vcode->setFontSize(30);
    12. $vcode->setDotNoise(40);
    13. $vcode->setLineNoise(2);
    14. //返回验证码图片IO流
    15. $rs = $vcode->paint();
    16. $base64 = chunk_split(base64_encode($rs['imgIo']));
    17. $encode = '<img src="data:image/jpg/png/gif;base64,'.$base64.'">';
    18. $return['img'] = $base64;
    19. $return['code'] = $rs['md5Code'];
    20. <?php
    21. /**
    22. * Verification Code Class
    23. *
    24. * Used to anti-spam base at PHP GD Lib
    25. * @author Eric,<wangyingei@yeah.net>
    26. * @version 1.0
    27. * @copyright Ereesoft Inc. 2009
    28. * @update 2009-05-14 22:32:05
    29. * @example
    30. * session_sratr();
    31. * $vcode = new Vcode();
    32. * $vcode->setLength(5);
    33. * $_SESSION['passport'] = $vcode->paint();// To be encrypted by MD5
    34. * @update bfire.lai 2015-07-08
    35. */
    36. class Vcode{
    37. /**
    38. * @var $width The width of the image,auto Calculated 验证图片宽度,程序自动计算
    39. */
    40. private $width;
    41. /**
    42. * @var $height Image height 验证图片高度
    43. */
    44. private $height;
    45. /**
    46. * @var $length Verification Code lenght 验证码长度
    47. */
    48. private $length;
    49. /**
    50. * @var $bgColor Image background color default random 验证图片背景色
    51. */
    52. private $bgColor;
    53. /**
    54. * @var $fontColor The text color 验证码颜色
    55. */
    56. private $fontColor;
    57. /**
    58. * @var $fontSize The text size 验证码字体大小
    59. */
    60. private $fontSize;
    61. /**
    62. * @var $showBorder 是否要边框
    63. */
    64. private $showBorder;
    65. /**
    66. * @var $dotNoise The number of noise 噪点数量
    67. */
    68. private $dotNoise;
    69. /**
    70. * @var $lineNoise The number of noise lines 干扰线数量
    71. */
    72. private $lineNoise;
    73. /**
    74. * @var $im image resource GD图像操作资源
    75. */
    76. private $im;
    77. private $result;
    78. /**
    79. * void Vcode::Vcode()
    80. *
    81. * The constructor
    82. */
    83. public function Vcode(){
    84. $this->dotNoise = 40;//初始化噪点数量
    85. $this->lineNoise = 0;//初始化干扰线数量
    86. }
    87. /**
    88. * void Vcode::setLength(integer $length)
    89. *
    90. * Set Verification Code length
    91. * @access public
    92. * @param integer $length;
    93. * @return void
    94. */
    95. public function setLength($length){
    96. $this->length = $length;
    97. }
    98. /**
    99. * void Vcode::setBgColor(string $bgColor)
    100. *
    101. * Set background color of the Verification Image
    102. * @access public
    103. * @param string $bgColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写
    104. * @return void
    105. */
    106. public function setBgColor($bgColor){
    107. $this->bgColor = sscanf($bgColor, '#%2x%2x%2x');
    108. }
    109. /**
    110. * void Vcode::setFontColor(string $fontgColor)
    111. *
    112. * Set text color of the Verification Image
    113. * @access public
    114. * @param string $fontColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写
    115. * @return void
    116. */
    117. public function setFontColor($fontColor){
    118. $this->fontColor = sscanf($fontColor, '#%2x%2x%2x');
    119. }
    120. public function setFontSize($fontSize){
    121. $this->fontSize = $fontSize;
    122. }
    123. //@设置是否显示边框
    124. public function setShowBorder($border){
    125. $this->showBorder=$border;
    126. }
    127. /**
    128. * void Vcode::setDotNoise(integer $num)
    129. *
    130. * How many noise dots want to draw
    131. * @access public
    132. * @param integer $num Too much will lower performance
    133. * @return void
    134. */
    135. public function setDotNoise($num){
    136. $this->dotNoise = $num;//手动设置噪点数量后,会覆盖初始设置
    137. }
    138. /**
    139. * void Vcode::setLineNoise(integer $num)
    140. *
    141. * How many noise lines want to draw
    142. * @access public
    143. * @param integer $num Too much will lower performance
    144. * @return void
    145. */
    146. public function setLineNoise($num){
    147. $this->lineNoise = $num;//手动设置干扰线数量后,会覆盖初始设置
    148. }
    149. /**
    150. * void Vcode::setWidth(integer $num)
    151. *
    152. * How many noise lines want to draw
    153. * @access public
    154. * @param integer $num Too much will lower performance
    155. * @return void
    156. */
    157. public function setWidth($num){
    158. $this->width = $num;//手动设置宽度
    159. }
    160. /**
    161. * void Vcode::setHeight(integer $num)
    162. *
    163. * How many noise lines want to draw
    164. * @access public
    165. * @param integer $num Too much will lower performance
    166. * @return void
    167. */
    168. public function setHeight($num){
    169. $this->height = $num;//手动设置高度
    170. }
    171. /**
    172. * String Vcode::randString()
    173. *
    174. * Create Random characters 生成随机字符串
    175. * @access private
    176. * @return String
    177. */
    178. private function randString( ){
    179. //$string = strtoupper(md5(microtime().mt_rand(0,9)));
    180. $a = new createnumber($this->length);
    181. $res = $a->createcode();
    182. $str = explode('|',$res);
    183. $this->result = $str[0];
    184. return $str[1];
    185. // return substr($string, 0, $this->length);
    186. }
    187. /**
    188. * void Vcode::drawDot()
    189. *
    190. * Draw dots noise 根据制定的数量随机画噪点,噪点颜色也随机
    191. * @access private
    192. */
    193. private function drawDot(){
    194. for($i=0; $i<$this->dotNoise; $i++){
    195. $color = imagecolorallocate($this->im,
    196. 101,
    197. 101,
    198. 254);//生成随机颜色
    199. imagesetpixel($this->im,
    200. mt_rand(0,$this->width),
    201. mt_rand((($this->height)/2),$this->height),
    202. $color);//在随机生成的坐标上画噪点
    203. }
    204. }
    205. /**
    206. * void Vcode::drawLine()
    207. *
    208. * Draw line noise 随机颜色随机画干扰线
    209. * @access private
    210. */
    211. private function drawLine(){
    212. for($i=0; $i<$this->lineNoise; $i++){
    213. $color = imagecolorallocate($this->im,
    214. 101,
    215. 101,
    216. 254);//随机生成颜色
    217. imagesetthickness($this->im,3);
    218. imageline($this->im,
    219. mt_rand(0,(($this->width)/4)),
    220. mt_rand(0,(($this->height)/4)),
    221. mt_rand((($this->width)/4),$this->width),
    222. mt_rand((($this->width)/4),(($this->height)/2)),
    223. $color);//在随机生成的坐标上画干扰线
    224. }
    225. }
    226. /**
    227. * String Vcode::paint()
    228. *
    229. * Create image and output
    230. * @access public
    231. * @return string The Verification Code to be encrypted by MD5
    232. */
    233. public function paint(){
    234. if(empty($this->length)) $this->length = 8;//验证码默认长度为8
    235. if(empty($this->fontSize)) $this->fontSize=6;
    236. if(empty($this->width)) $this->width = 150 ;//计算验证图片宽度
    237. if(empty($this->height)) $this->height =50;//制定验证码图片高度
    238. $this->im = imagecreate($this->width, $this->height);//创建画布
    239. if(empty($this->bgColor) || empty($this->fontColor)){//如果没有设置前景色和背景色则全部随机
    240. //避免前景色和背景色过于接近,背景色(0-130)的随机范围与前景色(131-255)分开
    241. imagecolorallocate( $this->im,
    242. mt_rand(0,130),
    243. mt_rand(0,130),
    244. mt_rand(0,130));
    245. $randString = $this->randString();
    246. for($i=0; $i<$this->length; $i++){
    247. $fontColor = imagecolorallocate($this->im,
    248. mt_rand(131,255),
    249. mt_rand(131,255),
    250. mt_rand(131,255));
    251. imagettftext($this->im,
    252. $this->fontSize,
    253. 10,
    254. $i*($this->fontSize-5)+10,
    255. mt_rand(30,35),
    256. $fontColor,
    257. 'fonts/arial.ttf',
    258. $randString{$i});
    259. //单个验证码字符高度随机,避免被OCR
    260. }
    261. } else {//如果设置了背景色和前景色,则不使用随机颜色
    262. imagecolorallocate( $this->im,
    263. $this->bgColor[0],
    264. $this->bgColor[1],
    265. $this->bgColor[2]);
    266. $randString = $this->randString();
    267. $fontColor = imagecolorallocate($this->im,
    268. $this->fontColor[0],
    269. $this->fontColor[1],
    270. $this->fontColor[2]);
    271. for($i=0;$i<$this->length;$i++){
    272. if($i==4){
    273. $angle = 0;
    274. }else{
    275. $angle = mt_rand(-30,30);
    276. }
    277. imagettftext($this->im,
    278. $this->fontSize,
    279. $angle,
    280. $i*($this->fontSize-5)+10,
    281. mt_rand(30,35),
    282. $fontColor,
    283. 'img/fonts/arial.ttf',
    284. $randString{$i});//每个验证码字符高度仍然随机
    285. /*
    286. //echo $this->im;
    287. echo $this->fontSize;
    288. echo "<br>";
    289. echo $i*10+10;
    290. echo "<br>";
    291. echo mt_rand(0,4);
    292. echo "<br>";
    293. echo $fontColor;
    294. echo "<br>";
    295. if(!is_file('fonts/arial.ttf')){echo true;}
    296. echo "<br>";
    297. echo $randString{$i};
    298. echo "<br>";
    299. */
    300. }
    301. }
    302. $this->drawDot();//绘制噪点
    303. $this->drawLine();//绘制干扰线
    304. if($this->showBorder){imagerectangle($this->im,0,0,$this->width-1,$this->height-1,$fontColor);} //添加边框
    305. // imagepng($this->im);
    306. // imagedestroy($this->im);
    307. // return md5($this->result);//返回MD5加密后的验证码,可直接放入session
    308. // $imgIo ='code_tmp.png';
    309. // imagepng($this->im,$imgIo);
    310. // imagedestroy($this->im);
    311. ob_start();
    312. imagepng($this->im);
    313. $content = ob_get_contents();
    314. ob_end_clean();
    315. $rs['imgIo'] = $content;
    316. $rs['md5Code'] = md5($this->result);
    317. return $rs;//返回MD5加密后的验证码,可直接放入session
    318. }
    319. }
    320. class createnumber{
    321. private $operator = '+-';
    322. private $str1 ="";
    323. private $str2 ="";
    324. // function __construct() {
    325. // $this->str1 = rand(2000,9000);
    326. // $this->str2 = rand(1,9);
    327. // }
    328. //
    329. // function createcode(){
    330. // $ope1 = $this->operator{mt_rand(0,1)};
    331. // $code1 = $this->str1;
    332. // $code2 = $this->str2;
    333. // $string = $code1.$ope1.$code2."=?";//生成字符串
    334. // if($ope1 == '+'){
    335. // $result = $this->str1+$this->str2;
    336. // }else if($ope1 == '-'){
    337. // $result = $this->str1-$this->str2;
    338. // }
    339. // return $result."|".$string ;
    340. // }
    341. function __construct($length) {
    342. //$this->str1 = rand(2000,9000);
    343. //$this->str2 = rand(1,9);
    344. $str = 'qwertyuipasdfghjkzxcvbnm';
    345. $len = strlen($str)-1;
    346. for($i=0;$i<$length;$i++)
    347. {
    348. $num = mt_rand(0,$len);
    349. $this->str1 = $this->str1.$str[$num];
    350. }
    351. }
    352. function createcode(){
    353. //$ope1 = $this->operator{mt_rand(0,1)};
    354. $code1 = $this->str1;
    355. //$code2 = $this->str2;
    356. //$string = $code1.$ope1.$code2."=?";//生成字符串
    357. $string = $code1;
    358. $result = $code1;
    359. // if($ope1 == '+'){
    360. // $result = $this->str1+$this->str2;
    361. // }else if($ope1 == '-'){
    362. // $result = $this->str1-$this->str2;
    363. // }
    364. return $result."|".$string ;
    365. }
    366. }
    367. ?>