PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/library/utility/verifycode/PwGDCode.php

https://github.com/cuijinquan/nextwind
PHP | 282 lines | 222 code | 37 blank | 23 comment | 28 complexity | 8049255841744c5f3a281e803ef9910a MD5 | raw file
  1. <?php
  2. Wind::import('LIB:utility.verifycode.PwBaseCode');
  3. /**
  4. * GD库验证码
  5. *
  6. * the last known user to change this file in the repository <$LastChangedBy: gao.wanggao $>
  7. * @author $Author: gao.wanggao $ foxsee@aliyun.com
  8. * @version $Id: PwGDCode.php 23362 2013-01-09 04:19:41Z gao.wanggao $
  9. * @package
  10. */
  11. class PwGDCode extends PwBaseCode {
  12. private static $_background;
  13. private static $_graph;
  14. private static $_font;
  15. private static $_size;
  16. private static $_angle;
  17. private static $_color;
  18. private static $_codeLen = 0;
  19. private static $_codeX = 0;
  20. private static $_codeY = 0;
  21. private static $_image;
  22. public static function init() {
  23. if(!function_exists("imagecreatetruecolor") && !function_exists("imagecolorallocate") && !function_exists("imagestring") && !function_exists("imagepng") && !function_exists("imagesetpixel") && !function_exists("imagefilledrectangle") && !function_exists("imagerectangle")) {
  24. return false;
  25. }
  26. self::setRandCode();
  27. return true;
  28. }
  29. public static function outputImage() {
  30. return self::$isRandGif ? (self::_outGif()) : (self::_outPng());
  31. }
  32. public static function outputFlash() {
  33. return self::_outFlash();
  34. }
  35. private static function _setRandX() {
  36. self::$_codeX = floor((self::$verifyWidth -20) / self::$_codeLen);
  37. }
  38. private static function _setRandY() {
  39. $_y = self::$verifyHeight/2;
  40. self::$_codeY = mt_rand($_y+5,$_y+10);
  41. }
  42. private static function _setRandBackground() {
  43. $red = self::$isRandBackground ? mt_rand(100, 200) : 255;
  44. $green = self::$isRandBackground ? mt_rand(100, 200) : 255;
  45. $blue = self::$isRandBackground ? mt_rand(100, 200) : 255;
  46. $alpha = 0;//self::$isRandBackground ? mt_rand(0, 127) : 0;
  47. self::$_background = array('red'=>$red,'green'=>$green,'blue'=>$blue,'alpha'=>$alpha);
  48. }
  49. private static function _setPicBackground() {
  50. $bgs=self::getVerifyBackground();
  51. $rand = array_rand($bgs);
  52. $imbg = imagecreatefromjpeg($bgs[$rand]);
  53. if (!$imbg) return false;
  54. imagecopymerge(self::$_image, $imbg, 0, 0, mt_rand(0,450-self::$verifyWidth), mt_rand(0,150-self::$verifyHeight), self::$verifyWidth, self::$verifyHeight, 100);
  55. imagedestroy($imbg);
  56. }
  57. private static function _setRandFont() {
  58. $_path=Wind::getRealDir(self::$path.'.font');
  59. if (self::$verifyType < 5) {
  60. $_fontList= self::getEnFontList();
  61. } else {
  62. $_fontList = self::getCnFontList();
  63. }
  64. $key = self::$isRandFont ? array_rand($_fontList,1) : 0;
  65. self::$_font = $_path.'/'.$_fontList[$key];
  66. return;
  67. }
  68. private static function _setRandSize() {
  69. self::$_size = self::$isRandSize ? mt_rand(14,20) : 18;
  70. }
  71. private static function _setRandAngle() {
  72. self::$_angle = self::$isRandAngle ? mt_rand(-20, 10) : 0;
  73. }
  74. private static function _setRandColor() {
  75. if (!self::$isRandColor) {
  76. self::$_color = imagecolorallocate(self::$_image, 0, 0, 0);
  77. } else {
  78. self::$_color = imagecolorallocate(self::$_image, mt_rand(0, 255), mt_rand(0, 120), mt_rand(0, 255));
  79. }
  80. }
  81. private static function _setRandDistortion() {
  82. if (!self::$isRandDistortion) return true;
  83. $_tmp = self::$_image;
  84. self::$verifyWidth = self::$verifyWidth;
  85. self::_creatImage();
  86. self::_creatBackground();
  87. for ( $i=0; $i<self::$verifyWidth; $i++) {
  88. for ( $j=0; $j<self::$verifyHeight; $j++) {
  89. $_color = imagecolorat($_tmp, $i , $j);
  90. if( intval($i+sin($j/self::$verifyHeight*2*M_PI)*10) <= self::$verifyWidth && intval(($i+sin($j/self::$verifyHeight*2*M_PI)*10)) >=0 ) {
  91. imagesetpixel(self::$_image, intval($i+sin($j/self::$verifyHeight*2*M_PI-0.6)*5), $j,$_color);
  92. }
  93. }
  94. }
  95. }
  96. private static function _setRandGraph() {
  97. if (!self::$isRandGraph) return true;
  98. $_tmp = mt_rand(1,3);
  99. switch ($_tmp) {
  100. case '1':
  101. self::_setImageLine();
  102. break;
  103. case '2':
  104. self::_setImagePix();
  105. break;
  106. case '3':
  107. self::_setImageEarc();
  108. break;
  109. /* case '4':
  110. self::_setImageLine();
  111. self::_setImageEarc();
  112. break;
  113. case '5':
  114. self::_setImageLine();
  115. //self::_setImagePix();
  116. break;
  117. case '6':
  118. //self::_setImagePix();
  119. self::_setImageEarc();
  120. break;*/
  121. }
  122. }
  123. private static function _setImageLine() {
  124. $_tmp = mt_rand(30,40);
  125. for ($i = 0; $i < $_tmp; $i++) {
  126. $_x = mt_rand(0, self::$verifyWidth);
  127. $_y = mt_rand(0, self::$verifyHeight);
  128. $_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
  129. imageline ( self::$_image, $_x, $_y, mt_rand(($_x-10), ($_x+5)), mt_rand(($_y+5), ($_y+20)), $_color);
  130. }
  131. }
  132. private static function _setImagePix() {
  133. $_tmp = mt_rand(600,800);
  134. for ($i = 0; $i < $_tmp; $i++) {
  135. $_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
  136. imagesetpixel(self::$_image, mt_rand(0, self::$verifyWidth), mt_rand(0, self::$verifyHeight), $_color);
  137. }
  138. }
  139. private static function _setImageEarc() {
  140. $_tmp = mt_rand(5,10);
  141. for ($i = 0; $i < $_tmp; $i++) {
  142. $_color = imagecolorallocate(self::$_image, mt_rand(50, 255), mt_rand(50, 200), mt_rand(25, 200));
  143. imagearc(self::$_image, mt_rand(0, self::$verifyWidth), mt_rand(10,self::$verifyHeight), mt_rand(10, self::$verifyWidth), mt_rand(self::$verifyHeight, self::$verifyHeight*2), mt_rand(0, 90), mt_rand(0,90), $_color);
  144. }
  145. }
  146. private static function _getCodeLenth() {
  147. //self::$_codeLen = Pw::strlen(self::$verifyCode);
  148. self::$_codeLen = WindString::strlen(self::$verifyCode, 'utf-8');
  149. }
  150. private static function _outFlash() {
  151. if (!class_exists('SWFBitmap')) return false;
  152. self::_getCodeLenth();
  153. self::_creatImage();
  154. self::_setRandBackground();
  155. self::_creatBackground();
  156. //self::_setPicBackground();
  157. self::_setRandFont();
  158. self::_setRandGraph();
  159. self::_writeImage();
  160. self::_setRandDistortion();
  161. $_tmpPath = Wind::getRealDir('DATA:tmp.');
  162. $_tmp = $_tmpPath.WindUtility::generateRandStr(8).'.png';
  163. imagepng(self::$_image, $_tmp);
  164. if (!WindFile::isFile($_tmp)) return false;
  165. imagedestroy(self::$_image);
  166. $bit= new SWFBitmap($_tmp);
  167. $shape= new SWFShape();
  168. $shape->setRightFill($shape->addFill($bit));
  169. $shape->drawLine($bit->getWidth(),0);
  170. $shape->drawLine(0,$bit->getHeight());
  171. $shape->drawLine(-$bit->getWidth(),0);
  172. $shape->drawLine(0, -$bit->getHeight());
  173. $movie= new SWFMovie();
  174. $movie->setDimension($bit->getWidth(),$bit->getHeight());
  175. $flash=$movie->add($shape);
  176. header("Pragma:no-cache");
  177. header("Cache-control:no-cache");
  178. header('Content-type: application/x-shockwave-flash');
  179. $movie->output();
  180. WindFolder::clear($_tmpPath);
  181. }
  182. private static function _outPng() {
  183. header("Pragma:no-cache");
  184. header("Cache-control:no-cache");
  185. header("Content-type: image/png");
  186. self::_getCodeLenth();
  187. self::_creatImage();
  188. if (self::$isRandBackground) {
  189. self::_setPicBackground();
  190. } else {
  191. self::_setRandBackground();
  192. self::_creatBackground();
  193. }
  194. self::_setRandFont();
  195. self::_setRandGraph();
  196. self::_writeImage();
  197. self::_setRandDistortion();
  198. imagepng(self::$_image);
  199. imagedestroy(self::$_image);
  200. }
  201. private static function _outGif() {
  202. header("Pragma:no-cache");
  203. header("Cache-control:no-cache");
  204. header("Content-type: image/gif");
  205. Wind::import('LIB:utility.verifycode.GifMerge');
  206. self::_getCodeLenth();
  207. self::_setRandBackground();
  208. self::_setRandFont();
  209. for ($i=0; $i<3; $i++) {
  210. self::_creatImage();
  211. self::_creatBackground();
  212. self::_setRandGraph();
  213. self::_writeImage();
  214. ob_start();
  215. imageGif(self::$_image);
  216. $frame[] = ob_get_contents();
  217. $delay[] = 100;
  218. imagedestroy(self::$_image);
  219. ob_end_clean();
  220. }
  221. $gif = new GifMerge($frame, 0, 0, 0, 0, $delay, 0, 0, 'C_MEMORY');
  222. echo $gif->getAnimation();
  223. }
  224. private static function _writeImage() {
  225. for ($i = 0; $i < self::$_codeLen; $i++) {
  226. //$_text = Pw::substrs(self::$verifyCode, 1, $i, false);
  227. $_text = WindString::substr(self::$verifyCode, $i, 1, 'utf-8', false);
  228. self::_setRandSize();
  229. self::_setRandAngle();
  230. self::_setRandX();
  231. self::_setRandY();
  232. self::_setRandColor();
  233. ImageTTFText(self::$_image, self::$_size, self::$_angle, (self::$_codeX * $i +10) , self::$_codeY, self::$_color, self::$_font,$_text);
  234. }
  235. }
  236. private static function _creatImage() {
  237. self::$_image = imagecreatetruecolor(self::$verifyWidth, self::$verifyHeight);
  238. imagesavealpha(self::$_image, true);
  239. }
  240. private static function _creatBackground() {
  241. $_tmp = imagecolorallocatealpha(self::$_image, self::$_background['red'], self::$_background['green'], self::$_background['blue'], self::$_background['alpha']);
  242. imagefill(self::$_image, 20, 0, $_tmp);
  243. }
  244. }
  245. ?>