PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/startZine/src/main/core/class/verify.class.php

http://ownerpress.googlecode.com/
PHP | 408 lines | 368 code | 27 blank | 13 comment | 77 complexity | a6ddd04a2a29cd9c757336d842901594 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ?????????? (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * verify.class.php
  9. *
  10. * ??????
  11. *
  12. * @package class
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. class Verify
  16. {
  17. var $code;
  18. var $type = 0;
  19. var $width = 90;
  20. var $height = 36;
  21. var $background = 1;
  22. var $adulterate = 1;
  23. var $ttf = 0;
  24. var $angle = 0;
  25. var $warping = 0;
  26. var $scatter = 0;
  27. var $color = 1;
  28. var $size = 0;
  29. var $shadow = 1;
  30. var $fontcolor;
  31. var $im;
  32. function display()
  33. {
  34. $this->width = $this->width >= 80 && $this->width <= 200 ? $this->width : 90;
  35. $this->height = $this->height >= 30 && $this->height <= 80 ? $this->height : 36;
  36. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
  37. function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
  38. function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
  39. $this->image();
  40. } else {
  41. $this->bitmap();
  42. }
  43. }
  44. function image()
  45. {
  46. $bgcontent = $this->background();
  47. $this->im = imagecreatefromstring($bgcontent);
  48. $this->adulterate && $this->adulterate();
  49. $this->ttf && function_exists('imagettftext') ? $this->ttffont() : $this->giffont();
  50. $this->scatter && $this->scatter($this->im);
  51. if(function_exists('imagepng'))
  52. {
  53. header('Content-type: image/png');
  54. imagepng($this->im);
  55. }
  56. else
  57. {
  58. header('Content-type: image/jpeg');
  59. imagejpeg($this->im, '', 100);
  60. }
  61. imagedestroy($this->im);
  62. }
  63. function background()
  64. {
  65. $this->im = imagecreatetruecolor($this->width, $this->height);
  66. $backgrounds = $c = array();
  67. if($this->background && function_exists('imagecreatefromjpeg') && function_exists('imagecolorat') && function_exists('imagecopymerge') &&
  68. function_exists('imagesetpixel') && function_exists('imageSX') && function_exists('imageSY'))
  69. {
  70. if($handle = @opendir(PUBLIC_ROOT.'./verify/background/'))
  71. {
  72. while($bgfile = @readdir($handle))
  73. {
  74. if(preg_match('/\.jpg$/i', $bgfile))
  75. {
  76. $backgrounds[] = PUBLIC_ROOT.'./verify/background/'.$bgfile;
  77. }
  78. }
  79. @closedir($handle);
  80. }
  81. if($backgrounds)
  82. {
  83. $imwm = imagecreatefromjpeg($backgrounds[array_rand($backgrounds)]);
  84. $colorindex = imagecolorat($imwm, 0, 0);
  85. $c = imagecolorsforindex($imwm, $colorindex);
  86. $colorindex = imagecolorat($imwm, 1, 0);
  87. imagesetpixel($imwm, 0, 0, $colorindex);
  88. $c[0] = $c['red'];$c[1] = $c['green'];$c[2] = $c['blue'];
  89. imagecopymerge($this->im, $imwm, 0, 0, mt_rand(0, 200 - $this->width), mt_rand(0, 80 - $this->height), imageSX($imwm), imageSY($imwm), 100);
  90. imagedestroy($imwm);
  91. }
  92. }
  93. if(!$this->background || !$backgrounds)
  94. {
  95. for($i = 0;$i < 3;$i++)
  96. {
  97. $start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];
  98. }
  99. for($i = 0;$i < $this->width;$i++)
  100. {
  101. $color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);
  102. imageline($this->im, $i, 0, $i, $this->height, $color);
  103. $c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
  104. }
  105. $c[0] -= 20;$c[1] -= 20;$c[2] -= 20;
  106. }
  107. ob_start();
  108. if(function_exists('imagepng'))
  109. {
  110. imagepng($this->im);
  111. }
  112. else
  113. {
  114. imagejpeg($this->im, '', 100);
  115. }
  116. imagedestroy($this->im);
  117. $bgcontent = ob_get_contents();
  118. ob_end_clean();
  119. $this->fontcolor = $c;
  120. return $bgcontent;
  121. }
  122. function adulterate()
  123. {
  124. $linenums = $this->height / 10;
  125. for($i = 0; $i <= $linenums;$i++) {
  126. $color = $this->color ? imagecolorallocate($this->im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)) : imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  127. $x = mt_rand(0, $this->width);
  128. $y = mt_rand(0, $this->height);
  129. if(mt_rand(0, 1)) {
  130. $w = mt_rand(0, $this->width);
  131. $h = mt_rand(0, $this->height);
  132. $s = mt_rand(0, 360);
  133. $e = mt_rand(0, 360);
  134. for($j = 0;$j < 3;$j++) {
  135. imagearc($this->im, $x + $j, $y, $w, $h, $s, $e, $color);
  136. }
  137. } else {
  138. $xe = mt_rand(0, $this->width);
  139. $ye = mt_rand(0, $this->height);
  140. imageline($this->im, $x, $y, $xe, $ye, $color);
  141. for($j = 0;$j < 3;$j++) {
  142. imageline($this->im, $x + $j, $y, $xe, $ye, $color);
  143. }
  144. }
  145. }
  146. }
  147. function adulteratefont()
  148. {
  149. $seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
  150. $x = $this->width / 4;
  151. $y = $this->height / 10;
  152. $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  153. for($i = 0; $i <= 3; $i++) {
  154. $adulteratecode = $seccodeunits[mt_rand(0, 23)];
  155. imagechar($this->im, 5, $x * $i + mt_rand(0, $x - 10), mt_rand($y, $this->height - 10 - $y), $adulteratecode, $text_color);
  156. }
  157. }
  158. function ttffont()
  159. {
  160. $seccode = $this->code;
  161. $seccoderoot = PUBLIC_ROOT.'./verify/font/';
  162. $dirs = opendir($seccoderoot);
  163. $seccodettf = array();
  164. while($entry = readdir($dirs))
  165. {
  166. if($entry != '.' && $entry != '..' && in_array(strtolower(fileext($entry)), array('ttf', 'ttc')))
  167. {
  168. $seccodettf[] = $entry;
  169. }
  170. }
  171. if(empty($seccodettf))
  172. {
  173. $this->giffont();
  174. return;
  175. }
  176. $seccodelength = 4;
  177. $widthtotal = 0;
  178. for($i = 0; $i < $seccodelength; $i++)
  179. {
  180. $font[$i]['font'] = $seccoderoot.$seccodettf[array_rand($seccodettf)];
  181. $font[$i]['angle'] = $this->angle ? mt_rand(-30, 30) : 0;
  182. $font[$i]['size'] = $this->type ? $this->width / 7 : $this->width / 6;
  183. $this->size && $font[$i]['size'] = mt_rand($font[$i]['size'] - $this->width / 40, $font[$i]['size'] + $this->width / 20);
  184. $box = imagettfbbox($font[$i]['size'], 0, $font[$i]['font'], $seccode[$i]);
  185. $font[$i]['zheight'] = max($box[1], $box[3]) - min($box[5], $box[7]);
  186. $box = imagettfbbox($font[$i]['size'], $font[$i]['angle'], $font[$i]['font'], $seccode[$i]);
  187. $font[$i]['height'] = max($box[1], $box[3]) - min($box[5], $box[7]);
  188. $font[$i]['hd'] = $font[$i]['height'] - $font[$i]['zheight'];
  189. $font[$i]['width'] = (max($box[2], $box[4]) - min($box[0], $box[6])) + mt_rand(0, $this->width / 8);
  190. $font[$i]['width'] = $font[$i]['width'] > $this->width / $seccodelength ? $this->width / $seccodelength : $font[$i]['width'];
  191. $widthtotal += $font[$i]['width'];
  192. }
  193. $x = mt_rand($font[0]['angle'] > 0 ? cos(deg2rad(90 - $font[0]['angle'])) * $font[0]['zheight'] : 1, $this->width - $widthtotal);
  194. !$this->color && $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  195. for($i = 0; $i < $seccodelength; $i++)
  196. {
  197. if($this->color)
  198. {
  199. $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  200. $this->shadow && $text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
  201. $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  202. }
  203. elseif($this->shadow)
  204. {
  205. $text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
  206. }
  207. $y = $font[0]['angle'] > 0 ? mt_rand($font[$i]['height'], $this->height) : mt_rand($font[$i]['height'] - $font[$i]['hd'], $this->height - $font[$i]['hd']);
  208. $this->shadow && imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x + 1, $y + 1, $text_shadowcolor, $font[$i]['font'], $seccode[$i]);
  209. imagettftext($this->im, $font[$i]['size'], $font[$i]['angle'], $x, $y, $text_color, $font[$i]['font'], $seccode[$i]);
  210. $x += $font[$i]['width'];
  211. }
  212. $this->warping && $this->warping($this->im);
  213. }
  214. function warping(&$obj)
  215. {
  216. $rgb = array();
  217. $direct = rand(0, 1);
  218. $width = imagesx($obj);
  219. $height = imagesy($obj);
  220. $level = $width / 20;
  221. for($j = 0;$j < $height;$j++)
  222. {
  223. for($i = 0;$i < $width;$i++)
  224. {
  225. $rgb[$i] = imagecolorat($obj, $i , $j);
  226. }
  227. for($i = 0;$i < $width;$i++)
  228. {
  229. $r = sin($j / $height * 2 * M_PI - M_PI * 0.5) * ($direct ? $level : -$level);
  230. imagesetpixel($obj, $i + $r , $j , $rgb[$i]);
  231. }
  232. }
  233. }
  234. function scatter(&$obj, $level = 0)
  235. {
  236. $rgb = array();
  237. $this->scatter = $leval ? $level : $this->scatter;
  238. $width = imagesx($obj);
  239. $height = imagesy($obj);
  240. for($j = 0;$j < $height;$j++)
  241. {
  242. for($i = 0;$i < $width;$i++)
  243. {
  244. $rgb[$i] = imagecolorat($obj, $i , $j);
  245. }
  246. for($i = 0;$i < $width;$i++)
  247. {
  248. $r = rand(-$this->scatter, $this->scatter);
  249. imagesetpixel($obj, $i + $r , $j , $rgb[$i]);
  250. }
  251. }
  252. }
  253. function giffont()
  254. {
  255. $seccode = $this->code;
  256. $seccodedir = array();
  257. if(function_exists('imagecreatefromgif'))
  258. {
  259. $seccoderoot = PUBLIC_ROOT.'./verify/gif/';
  260. $dirs = opendir($seccoderoot);
  261. while($dir = readdir($dirs))
  262. {
  263. if($dir != '.' && $dir != '..' && file_exists($seccoderoot.$dir.'/9.gif'))
  264. {
  265. $seccodedir[] = $dir;
  266. }
  267. }
  268. }
  269. $widthtotal = 0;
  270. for($i = 0; $i <= 3; $i++)
  271. {
  272. $this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
  273. if(!empty($this->imcodefile) && file_exists($this->imcodefile))
  274. {
  275. $font[$i]['file'] = $this->imcodefile;
  276. $font[$i]['data'] = getimagesize($this->imcodefile);
  277. $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
  278. $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
  279. $font[$i]['width'] += mt_rand(0, $this->width / 5 - $font[$i]['width']);
  280. $widthtotal += $font[$i]['width'];
  281. }
  282. else
  283. {
  284. $font[$i]['file'] = '';
  285. $font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);
  286. $widthtotal += $font[$i]['width'];
  287. }
  288. }
  289. $x = mt_rand(1, $this->width - $widthtotal);
  290. for($i = 0; $i <= 3; $i++)
  291. {
  292. $this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  293. if($font[$i]['file'])
  294. {
  295. $this->imcode = imagecreatefromgif($font[$i]['file']);
  296. if($this->size)
  297. {
  298. $font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this->width / 20);
  299. $font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this->width / 20);
  300. }
  301. $y = mt_rand(0, $this->height - $font[$i]['height']);
  302. if($this->shadow)
  303. {
  304. $this->imcodeshadow = $this->imcode;
  305. imagecolorset($this->imcodeshadow, 0, 0, 0, 0);
  306. imagecopyresized($this->im, $this->imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
  307. }
  308. imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  309. imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
  310. }
  311. else
  312. {
  313. $y = mt_rand(0, $this->height - 20);
  314. if($this->shadow)
  315. {
  316. $text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
  317. imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
  318. }
  319. $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  320. imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);
  321. }
  322. $x += $font[$i]['width'];
  323. }
  324. }
  325. function bitmap()
  326. {
  327. $numbers = array
  328. (
  329. 'B' => array('00','fc','66','66','66','7c','66','66','fc','00'),
  330. 'C' => array('00','38','64','c0','c0','c0','c4','64','3c','00'),
  331. 'E' => array('00','fe','62','62','68','78','6a','62','fe','00'),
  332. 'F' => array('00','f8','60','60','68','78','6a','62','fe','00'),
  333. 'G' => array('00','78','cc','cc','de','c0','c4','c4','7c','00'),
  334. 'H' => array('00','e7','66','66','66','7e','66','66','e7','00'),
  335. 'J' => array('00','f8','cc','cc','cc','0c','0c','0c','7f','00'),
  336. 'K' => array('00','f3','66','66','7c','78','6c','66','f7','00'),
  337. 'M' => array('00','f7','63','6b','6b','77','77','77','e3','00'),
  338. 'P' => array('00','f8','60','60','7c','66','66','66','fc','00'),
  339. 'Q' => array('00','78','cc','cc','cc','cc','cc','cc','78','00'),
  340. 'R' => array('00','f3','66','6c','7c','66','66','66','fc','00'),
  341. 'T' => array('00','78','30','30','30','30','b4','b4','fc','00'),
  342. 'V' => array('00','1c','1c','36','36','36','63','63','f7','00'),
  343. 'W' => array('00','36','36','36','77','7f','6b','63','f7','00'),
  344. 'X' => array('00','f7','66','3c','18','18','3c','66','ef','00'),
  345. 'Y' => array('00','7e','18','18','18','3c','24','66','ef','00'),
  346. '2' => array('fc','c0','60','30','18','0c','cc','cc','78','00'),
  347. '3' => array('78','8c','0c','0c','38','0c','0c','8c','78','00'),
  348. '4' => array('00','3e','0c','fe','4c','6c','2c','3c','1c','1c'),
  349. '6' => array('78','cc','cc','cc','ec','d8','c0','60','3c','00'),
  350. '7' => array('30','30','38','18','18','18','1c','8c','fc','00'),
  351. '8' => array('78','cc','cc','cc','78','cc','cc','cc','78','00'),
  352. '9' => array('f0','18','0c','6c','dc','cc','cc','cc','78','00')
  353. );
  354. foreach($numbers as $i => $number) {
  355. for($j = 0; $j < 6; $j++) {
  356. $a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
  357. $a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
  358. mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
  359. mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
  360. }
  361. }
  362. $bitmap = array();
  363. for($i = 0; $i < 20; $i++) {
  364. for($j = 0; $j <= 3; $j++) {
  365. $bytes = $numbers[$this->code[$j]][$i];
  366. $a = mt_rand(0, 14);
  367. array_push($bitmap, $bytes);
  368. }
  369. }
  370. for($i = 0; $i < 8; $i++) {
  371. $a = substr('012345', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
  372. array_unshift($bitmap, $a);
  373. array_push($bitmap, $a);
  374. }
  375. $image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
  376. '0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));
  377. header('Content-Type: image/bmp');
  378. echo $image;
  379. }
  380. }
  381. ?>