seccode.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. class seccode {
  3. public $code;
  4. public $width = 150;
  5. public $height = 60;
  6. public $background = 1;
  7. public $adulterate = 1;
  8. public $scatter = 0;
  9. public $color = 1;
  10. public $size = 0;
  11. public $shadow = 1;
  12. public $animator = 0;
  13. public $fontpath = '';
  14. public $datapath = '';
  15. public $includepath= '';
  16. public $fontcolor;
  17. public $im;
  18. public function display() {
  19. if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized') &&
  20. function_exists('imagecolorallocate') && function_exists('imagechar') && function_exists('imagecolorsforindex') &&
  21. function_exists('imageline') && function_exists('imagecreatefromstring') && (function_exists('imagegif') || function_exists('imagepng') || function_exists('imagejpeg'))) {
  22. $this->image();
  23. } else {
  24. exit('gd extension error');
  25. }
  26. }
  27. function image() {
  28. $bgcontent = $this->background();
  29. $this->im = imagecreatefromstring($bgcontent);
  30. $this->adulterate && $this->adulterate();
  31. $this->giffont();
  32. $this->scatter && $this->scatter($this->im);
  33. if(function_exists('imagepng')) {
  34. header('Content-type: image/png');
  35. imagepng($this->im);
  36. } else {
  37. header('Content-type: image/jpeg');
  38. imagejpeg($this->im, '', 100);
  39. }
  40. imagedestroy($this->im);
  41. }
  42. function background() {
  43. $this->im = imagecreatetruecolor($this->width, $this->height);
  44. $backgrounds = $c = array();
  45. if(!$this->background || !$backgrounds) {
  46. for($i = 0;$i < 3;$i++) {
  47. $start[$i] = mt_rand(200, 255);$end[$i] = mt_rand(100, 150);$step[$i] = ($end[$i] - $start[$i]) / $this->width;$c[$i] = $start[$i];
  48. }
  49. for($i = 0;$i < $this->width;$i++) {
  50. $color = imagecolorallocate($this->im, $c[0], $c[1], $c[2]);
  51. imageline($this->im, $i, 0, $i, $this->height, $color);
  52. $c[0] += $step[0];$c[1] += $step[1];$c[2] += $step[2];
  53. }
  54. $c[0] -= 20;$c[1] -= 20;$c[2] -= 20;
  55. }
  56. ob_start();
  57. if(function_exists('imagepng')) {
  58. imagepng($this->im);
  59. } else {
  60. imagejpeg($this->im, '', 100);
  61. }
  62. imagedestroy($this->im);
  63. $bgcontent = ob_get_contents();
  64. ob_end_clean();
  65. $this->fontcolor = $c;
  66. return $bgcontent;
  67. }
  68. /**
  69. * 画干扰线
  70. *
  71. */
  72. function adulterate() {
  73. $linenums = $this->height / 20;
  74. for($i = 0; $i <= $linenums;$i++) {
  75. $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]);
  76. $x = mt_rand(0, $this->width);
  77. $y = mt_rand(0, $this->height);
  78. if(mt_rand(0, 0)) {
  79. $w = mt_rand(0, $this->width);
  80. $h = mt_rand(0, $this->height);
  81. $s = mt_rand(0, 360);
  82. $e = mt_rand(0, 360);
  83. for($j = 0;$j < 3;$j++) {
  84. imagearc($this->im, $x + $j, $y, $w, $h, $s, $e, $color);
  85. }
  86. } else {
  87. $xe = mt_rand(0, $this->width);
  88. $ye = mt_rand(0, $this->height);
  89. imageline($this->im, $x, $y, $xe, $ye, $color);
  90. for($j = 0;$j < 3;$j++) {
  91. imageline($this->im, $x + $j, $y, $xe, $ye, $color);
  92. }
  93. }
  94. }
  95. }
  96. function scatter(&$obj, $level = 0) {
  97. $rgb = array();
  98. $this->scatter = $level ? $level : $this->scatter;
  99. $width = imagesx($obj);
  100. $height = imagesy($obj);
  101. for($j = 0;$j < $height;$j++) {
  102. for($i = 0;$i < $width;$i++) {
  103. $rgb[$i] = imagecolorat($obj, $i , $j);
  104. }
  105. for($i = 0;$i < $width;$i++) {
  106. $r = rand(-$this->scatter, $this->scatter);
  107. imagesetpixel($obj, $i + $r , $j , $rgb[$i]);
  108. }
  109. }
  110. }
  111. function giffont() {
  112. $seccode = $this->code;
  113. $seccodedir = array();
  114. if(function_exists('imagecreatefromgif')) {
  115. $seccoderoot = $this->datapath.'gif/';
  116. $dirs = opendir($seccoderoot);
  117. while($dir = readdir($dirs)) {
  118. if($dir != '.' && $dir != '..' && file_exists($seccoderoot.$dir.'/9.gif')) {
  119. $seccodedir[] = $dir;
  120. }
  121. }
  122. }
  123. $widthtotal = 0;
  124. for($i = 0; $i <= 3; $i++) {
  125. $this->imcodefile = $seccodedir ? $seccoderoot.$seccodedir[array_rand($seccodedir)].'/'.strtolower($seccode[$i]).'.gif' : '';
  126. if(!empty($this->imcodefile) && file_exists($this->imcodefile)) {
  127. $font[$i]['file'] = $this->imcodefile;
  128. $font[$i]['data'] = getimagesize($this->imcodefile);
  129. $font[$i]['width'] = $font[$i]['data'][0] + mt_rand(0, 6) - 4;
  130. $font[$i]['height'] = $font[$i]['data'][1] + mt_rand(0, 6) - 4;
  131. if (($this->width / 5 - $font[$i]['width'])>0){
  132. $tt = mt_rand(0, $this->width / 5 - $font[$i]['width']);
  133. }else{
  134. $tt = mt_rand($this->width / 5 - $font[$i]['width'],0);
  135. }
  136. $font[$i]['width'] += $tt;
  137. $widthtotal += $font[$i]['width'];
  138. } else {
  139. $font[$i]['file'] = '';
  140. $font[$i]['width'] = 8 + mt_rand(0, $this->width / 5 - 5);
  141. $widthtotal += $font[$i]['width'];
  142. }
  143. }
  144. $x = mt_rand(1, $this->width - $widthtotal);
  145. for($i = 0; $i <= 3; $i++) {
  146. $this->color && $this->fontcolor = array(mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
  147. if($font[$i]['file']) {
  148. $this->imcode = imagecreatefromgif($font[$i]['file']);
  149. if($this->size) {
  150. $font[$i]['width'] = mt_rand($font[$i]['width'] - $this->width / 20, $font[$i]['width'] + $this->width / 20);
  151. $font[$i]['height'] = mt_rand($font[$i]['height'] - $this->width / 20, $font[$i]['height'] + $this->width / 20);
  152. }
  153. if ($this->height - $font[$i]['height']>0){
  154. $y = mt_rand(0, $this->height - $font[$i]['height']);
  155. }else{
  156. $y = mt_rand($this->height - $font[$i]['height'],0);
  157. }
  158. if($this->shadow) {
  159. $this->imcodeshadow = $this->imcode;
  160. imagecolorset($this->imcodeshadow, 0, 0, 0, 0);
  161. 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]);
  162. }
  163. imagecolorset($this->imcode, 0 , $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  164. imagecopyresized($this->im, $this->imcode, $x, $y, 0, 0, $font[$i]['width'], $font[$i]['height'], $font[$i]['data'][0], $font[$i]['data'][1]);
  165. } else {
  166. $y = mt_rand(0, $this->height - 20);
  167. if($this->shadow) {
  168. $text_shadowcolor = imagecolorallocate($this->im, 0, 0, 0);
  169. imagechar($this->im, 5, $x + 1, $y + 1, $seccode[$i], $text_shadowcolor);
  170. }
  171. $text_color = imagecolorallocate($this->im, $this->fontcolor[0], $this->fontcolor[1], $this->fontcolor[2]);
  172. imagechar($this->im, 5, $x, $y, $seccode[$i], $text_color);
  173. }
  174. $x += $font[$i]['width'];
  175. }
  176. }
  177. }
  178. ?>