Code.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Passport\Lib;
  3. use Dever;
  4. class Code
  5. {
  6. private $width;
  7. private $height;
  8. public $code;
  9. public $mcode;
  10. public $string;
  11. private $image;
  12. public function __construct($width = '160', $height = '30')
  13. {
  14. $this->width = $width;
  15. $this->height = $height;
  16. }
  17. public function create()
  18. {
  19. header("Content-type: image/png");
  20. $this->createCode();
  21. $this->createImage();
  22. $this->createPix();
  23. $this->write();
  24. imagepng($this->image);
  25. imagedestroy($this->image);
  26. }
  27. public function createM()
  28. {
  29. $len = isset(Dever::config('base', 'project')->mobileCode['length']) ? Dever::config('base', 'project')->mobileCode['length'] : 4;
  30. $this->createRand($len);
  31. }
  32. private function createImage()
  33. {
  34. $this->image = @imagecreate($this->width, $this->height);
  35. $back = imagecolorallocate($this->image, 255, 255, 255);
  36. $border = imagecolorallocate($this->image, 0, 0, 0);
  37. imagefilledrectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $back);
  38. imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $border);
  39. }
  40. private function createPix()
  41. {
  42. for ($i = 0; $i <= 200; $i++) {
  43. imagesetpixel($this->image, rand(2, $this->width), rand(2, $this->height), imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));
  44. }
  45. }
  46. public function createRand($length)
  47. {
  48. $this->mcode = null;
  49. $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
  50. $str = '0123456789';
  51. $max = strlen($str) - 1;
  52. for ($i = 0; $i < $length; $i++) {
  53. $this->mcode .= $str[rand(0, $max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数
  54. }
  55. }
  56. private function createCode()
  57. {
  58. $cal = array
  59. (
  60. array('+', '+'),
  61. array('-', '-'),
  62. //array('*', '乘以'),
  63. //array('/', '除以'),
  64. );
  65. $index = array_rand($cal);
  66. $m1 = rand(1, 100);
  67. $m2 = rand(1, 100);
  68. $this->string = $m1 . $cal[$index][1] . $m2 . '';
  69. $this->code = '$this->code = ' . $m1 . $cal[$index][0] . $m2 . ';';
  70. eval($this->code);
  71. }
  72. private function write()
  73. {
  74. $length = strlen($this->string);
  75. for ($i = 0; $i < $length; $i++) {
  76. $bg_color = imagecolorallocate($this->image, rand(0, 255), rand(0, 128), rand(0, 255));
  77. $x = floor($this->width / $length) * $i;
  78. $y = rand(0, $this->height - 15);
  79. imagechar($this->image, rand(5, 5), $x + 5, $y, $this->string[$i], $bg_color);
  80. }
  81. }
  82. public function __destruct()
  83. {
  84. unset($this->width, $this->height, $this->image);
  85. }
  86. }