PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/miles/GlobalMBA/mba/sns_00/upload/home/source/do_seccode.php

#
PHP | 228 lines | 194 code | 24 blank | 10 comment | 44 complexity | 42adcf762cef92fe4f10e3ccf593db5a MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*
  3. [UCenter Home] (C) 2007-2008 Comsenz Inc.
  4. $Id: do_seccode.php 8531 2008-08-20 07:27:23Z liguode $
  5. */
  6. if(!defined('IN_UCHOME')) {
  7. exit('Access Denied');
  8. }
  9. //配置
  10. $seccodedata = array (
  11. 'width' => 100,
  12. 'height' => 40,
  13. 'adulterate' => '1',//随机背景图形
  14. 'angle' => '0',//随机倾斜度
  15. 'shadow' => '1',//阴影
  16. );
  17. //验证码
  18. $seccode = mkseccode();
  19. //设定cookie
  20. ssetcookie('seccode', authcode($seccode, 'ENCODE'));
  21. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
  22. function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
  23. function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
  24. $bgcontent = seccode_background();
  25. $im = imagecreatefromstring($bgcontent);
  26. if($seccodedata['adulterate']) {
  27. seccode_adulterate();
  28. }
  29. seccode_giffont();
  30. if(function_exists('imagepng')) {
  31. header('Content-type: image/png');
  32. imagepng($im);
  33. } else {
  34. header('Content-type: image/jpeg');
  35. imagejpeg($im, '', 100);
  36. }
  37. imagedestroy($im);
  38. } else {
  39. $numbers = array
  40. (
  41. 'B' => array('00','fc','66','66','66','7c','66','66','fc','00'),
  42. 'C' => array('00','38','64','c0','c0','c0','c4','64','3c','00'),
  43. 'E' => array('00','fe','62','62','68','78','6a','62','fe','00'),
  44. 'F' => array('00','f8','60','60','68','78','6a','62','fe','00'),
  45. 'G' => array('00','78','cc','cc','de','c0','c4','c4','7c','00'),
  46. 'H' => array('00','e7','66','66','66','7e','66','66','e7','00'),
  47. 'J' => array('00','f8','cc','cc','cc','0c','0c','0c','7f','00'),
  48. 'K' => array('00','f3','66','66','7c','78','6c','66','f7','00'),
  49. 'M' => array('00','f7','63','6b','6b','77','77','77','e3','00'),
  50. 'P' => array('00','f8','60','60','7c','66','66','66','fc','00'),
  51. 'Q' => array('00','78','cc','cc','cc','cc','cc','cc','78','00'),
  52. 'R' => array('00','f3','66','6c','7c','66','66','66','fc','00'),
  53. 'T' => array('00','78','30','30','30','30','b4','b4','fc','00'),
  54. 'V' => array('00','1c','1c','36','36','36','63','63','f7','00'),
  55. 'W' => array('00','36','36','36','77','7f','6b','63','f7','00'),
  56. 'X' => array('00','f7','66','3c','18','18','3c','66','ef','00'),
  57. 'Y' => array('00','7e','18','18','18','3c','24','66','ef','00'),
  58. '2' => array('fc','c0','60','30','18','0c','cc','cc','78','00'),
  59. '3' => array('78','8c','0c','0c','38','0c','0c','8c','78','00'),
  60. '4' => array('00','3e','0c','fe','4c','6c','2c','3c','1c','1c'),
  61. '6' => array('78','cc','cc','cc','ec','d8','c0','60','3c','00'),
  62. '7' => array('30','30','38','18','18','18','1c','8c','fc','00'),
  63. '8' => array('78','cc','cc','cc','78','cc','cc','cc','78','00'),
  64. '9' => array('f0','18','0c','6c','dc','cc','cc','cc','78','00')
  65. );
  66. foreach($numbers as $i => $number) {
  67. for($j = 0; $j < 6; $j++) {
  68. $a1 = substr('012', mt_rand(0, 2), 1).substr('012345', mt_rand(0, 5), 1);
  69. $a2 = substr('012345', mt_rand(0, 5), 1).substr('0123', mt_rand(0, 3), 1);
  70. mt_rand(0, 1) == 1 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a1);
  71. mt_rand(0, 1) == 0 ? array_push($numbers[$i], $a1) : array_unshift($numbers[$i], $a2);
  72. }
  73. }
  74. $bitmap = array();
  75. for($i = 0; $i < 20; $i++) {
  76. for($j = 0; $j < 4; $j++) {
  77. $n = substr($seccode, $j, 1);
  78. $bytes = $numbers[$n][$i];
  79. $a = mt_rand(0, 14);
  80. array_push($bitmap, $bytes);
  81. }
  82. }
  83. for($i = 0; $i < 8; $i++) {
  84. $a = substr('012345', mt_rand(0, 2), 1) . substr('012345', mt_rand(0, 5), 1);
  85. array_unshift($bitmap, $a);
  86. array_push($bitmap, $a);
  87. }
  88. $image = pack('H*', '424d9e000000000000003e000000280000002000000018000000010001000000'.
  89. '0000600000000000000000000000000000000000000000000000FFFFFF00'.implode('', $bitmap));
  90. header('Content-Type: image/bmp');
  91. echo $image;
  92. }
  93. //生成随机
  94. function mkseccode() {
  95. $seccode = random(6, 1);
  96. $s = sprintf('%04s', base_convert($seccode, 10, 24));
  97. $seccode = '';
  98. $seccodeunits = 'BCEFGHJKMPQRTVWXY2346789';
  99. for($i = 0; $i < 4; $i++) {
  100. $unit = ord($s{$i});
  101. $seccode .= ($unit >= 0x30 && $unit <= 0x39) ? $seccodeunits[$unit - 0x30] : $seccodeunits[$unit - 0x57];
  102. }
  103. return $seccode;
  104. }
  105. //背景
  106. function seccode_background() {
  107. global $seccodedata, $c;
  108. $im = imagecreatetruecolor($seccodedata['width'], $seccodedata['height']);
  109. $backgroundcolor = imagecolorallocate($im, 255, 255, 255);
  110. for($i = 0;$i < 3;$i++) {
  111. $start[$i] = mt_rand(200, 255);
  112. $end[$i] = mt_rand(100, 245);
  113. $step[$i] = ($end[$i] - $start[$i]) / $seccodedata['width'];
  114. $c[$i] = $start[$i];
  115. }
  116. //$color = imagecolorallocate($im, 235, 235, 235);
  117. for($i = 0;$i < $seccodedata['width'];$i++) {
  118. $color = imagecolorallocate($im, $c[0], $c[1], $c[2]);
  119. imageline($im, $i, 0, $i-$angle, $seccodedata['height'], $color);
  120. $c[0] += $step[0];
  121. $c[1] += $step[1];
  122. $c[2] += $step[2];
  123. }
  124. $c[0] -= 20;
  125. $c[1] -= 20;
  126. $c[2] -= 20;
  127. obclean();
  128. if(function_exists('imagepng')) {
  129. imagepng($im);
  130. } else {
  131. imagejpeg($im, '', 100);
  132. }
  133. imagedestroy($im);
  134. $bgcontent = ob_get_contents();
  135. obclean();
  136. return $bgcontent;
  137. }
  138. function seccode_adulterate() {
  139. global $seccodedata, $im, $c;
  140. $linenums = $seccodedata['height'] / 10;
  141. for($i=0; $i <= $linenums; $i++) {
  142. $color = imagecolorallocate($im, $c[0], $c[1], $c[2]);
  143. $x = mt_rand(0, $seccodedata['width']);
  144. $y = mt_rand(0, $seccodedata['height']);
  145. if(mt_rand(0, 1)) {
  146. imagearc($im, $x, $y, mt_rand(0, $seccodedata['width']), mt_rand(0, $seccodedata['height']), mt_rand(0, 360), mt_rand(0, 360), $color);
  147. } else {
  148. imageline($im, $x, $y, $linex + mt_rand(0, 20), $liney + mt_rand(0, mt_rand($seccodedata['height'], $seccodedata['width'])), $color);
  149. }
  150. }
  151. }
  152. function seccode_giffont() {
  153. global $seccode, $seccodedata, $im, $c;
  154. $seccodedir = array();
  155. if(function_exists('imagecreatefromgif')) {
  156. $seccoderoot = 'image/seccode/';
  157. $dirs = opendir($seccoderoot);
  158. while($dir = readdir($dirs)) {
  159. if($dir != '.' && $dir != '..' && file_exists($seccoderoot.$dir.'/9.gif')) {
  160. $seccodedir[] = $dir;
  161. }
  162. }
  163. }
  164. $widthtotal = 0;
  165. for($i = 0; $i <= 3; $i++) {
  166. $imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
  167. if(!empty($imcodefile) && file_exists($imcodefile)) {
  168. $font[$i]['file'] = $imcodefile;
  169. $font[$i]['data'] = getimagesize($imcodefile);
  170. $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
  171. $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
  172. $font[$i]['width'] += mt_rand(0, $seccodedata['width'] / 5 - $font[$i]['width']);
  173. $widthtotal += $font[$i]['width'];
  174. } else {
  175. $font[$i]['file'] = '';
  176. $font[$i]['width'] = 8 + mt_rand(0, $seccodedata['width'] / 5 - 5);
  177. $widthtotal += $font[$i]['width'];
  178. }
  179. }
  180. $x = mt_rand(1, $seccodedata['width'] - $widthtotal);
  181. for($i = 0; $i <= 3; $i++) {
  182. if($font[$i]['file']) {
  183. $imcode = imagecreatefromgif($font[$i]['file']);
  184. $y = mt_rand(0, $seccodedata['height'] - $font[$i]['height']);
  185. if($seccodedata['shadow']) {
  186. $imcodeshadow = $imcode;
  187. imagecolorset($imcodeshadow, 0 , 255 - $c[0], 255 - $c[1], 255 - $c[2]);
  188. imagecopyresized($im, $imcodeshadow, $x + 1, $y + 1, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
  189. }
  190. imagecolorset($imcode, 0 , $c[0], $c[1], $c[2]);
  191. imagecopyresized($im, $imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
  192. } else {
  193. $y = mt_rand(0, $seccodedata['height'] - 20);
  194. if($seccodedata['shadow']) {
  195. $text_shadowcolor = imagecolorallocate($im, 255 - $c[0], 255 - $c[1], 255 - $c[2]);
  196. imagechar($im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
  197. }
  198. $text_color = imagecolorallocate($im, $c[0], $c[1], $c[2]);
  199. imagechar($im, 5, $x, $y, $seccode[$i], $text_color);
  200. }
  201. $x += $font[$i]['width'];
  202. }
  203. }
  204. ?>