PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Common/Plugin/ImgCode.php

http://my-project-zf.googlecode.com/
PHP | 132 lines | 88 code | 12 blank | 32 comment | 13 complexity | 351cb72ac973dd864652a9a8b0a69215 MD5 | raw file
  1. <?php
  2. /**
  3. * ???????
  4. *
  5. */
  6. class Common_Plugin_ImgCode extends Zend_Controller_Plugin_Abstract {
  7. /**
  8. * ???????
  9. *
  10. * @static
  11. * @access public
  12. *
  13. * @param string $length ??
  14. * @param string $mode ?? 0 ?? 1:???2????? 3:???? 4:??????
  15. * @param string $type ????
  16. * @param string $width ??
  17. * @param string $height ??
  18. *
  19. * @return string
  20. * @throws ThinkExecption
  21. */
  22. function image($length = 4, $mode = 4, $type = 'png', $width = 50, $height = 25) {
  23. $randval = $this->rand_string($length, $mode); //?????
  24. $authCode = new Zend_Session_Namespace('Auth_Code'); //???????
  25. $authCode->imagecode = $randval; //??session????????session?
  26. //$_SESSION['IMGCODE']= $randval; //??????$_SESSION['IMGCODE']?
  27. $width = ($length * 9 + 10) > $width ? $length * 9 + 10 : $width;
  28. if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
  29. $im = @ imagecreatetruecolor($width, $height);
  30. } else {
  31. $im = @ imagecreate($width, $height);
  32. }
  33. $r = Array (
  34. 225,
  35. 255,
  36. 255,
  37. 223
  38. );
  39. $g = Array (
  40. 225,
  41. 236,
  42. 237,
  43. 255
  44. );
  45. $b = Array (
  46. 225,
  47. 236,
  48. 166,
  49. 125
  50. );
  51. $key = mt_rand(0, 3);
  52. $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //???????
  53. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  54. $pointColor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); //???
  55. @ imagefilledrectangle($im, 0, 0, $width -1, $height -1, $backColor);
  56. @ imagerectangle($im, 0, 0, $width -1, $height -1, $borderColor);
  57. $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
  58. // ??
  59. for ($i = 0; $i < 10; $i++) {
  60. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  61. imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
  62. }
  63. for ($i = 0; $i < 25; $i++) {
  64. $fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  65. imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pointColor);
  66. }
  67. @ imagestring($im, 5, 5, 3, $randval, $stringColor);
  68. $this->output($im, $type);
  69. }
  70. function output($im, $type = 'png') {
  71. header("Content-type: image/" . $type);
  72. $ImageFun = 'Image' . $type;
  73. $ImageFun ($im);
  74. imagedestroy($im);
  75. }
  76. /**
  77. * ???????????????? ????6? ???????
  78. *
  79. * @param string $len ??
  80. * @param string $type ????
  81. * 0 ?? 1 ?? ?? ??
  82. * @param string $addChars ????
  83. * @return string
  84. */
  85. function rand_string($len = 6, $type = '', $addChars = '') {
  86. $str = '';
  87. switch ($type) {
  88. case 0 :
  89. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
  90. break;
  91. case 1 :
  92. $chars = str_repeat('0123456789', 3);
  93. break;
  94. case 2 :
  95. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
  96. break;
  97. case 3 :
  98. $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
  99. break;
  100. default :
  101. // ????????????oOLl???01???????addChars??
  102. $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
  103. break;
  104. }
  105. if ($len > 10) { //?????????????
  106. $chars = $type == 1 ? str_repeat($chars, $len) : str_repeat($chars, 5);
  107. }
  108. if ($type != 4) {
  109. $chars = str_shuffle($chars);
  110. $str = substr($chars, 0, $len);
  111. } else {
  112. // ?????
  113. for ($i = 0; $i < $len; $i++) {
  114. $str .= substr($chars, floor(mt_rand(0, mb_strlen($chars, 'utf-8') - 1)), 1);
  115. }
  116. }
  117. return $str;
  118. }
  119. }
  120. ?>