chkcode.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace WY\app\controller;
  3. if (!defined('WY_ROOT')) {
  4. exit;
  5. }
  6. class chkcode
  7. {
  8. function index()
  9. {
  10. $randCode = '';
  11. $chars = '23456789';
  12. for ($i = 0; $i < 5; $i++) {
  13. $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  14. }
  15. $_SESSION['chkcode'] = strtolower($randCode);
  16. $img = imagecreate(70, 22);
  17. $bgColor = isset($_GET['mode']) && $_GET['mode'] == 't' ? imagecolorallocate($img, 245, 245, 245) : imagecolorallocate($img, 255, 255, 255);
  18. $pixColor = imagecolorallocate($img, mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250));
  19. for ($i = 0; $i < 5; $i++) {
  20. $x = $i * 13 + mt_rand(0, 4) - 2;
  21. $y = mt_rand(0, 3);
  22. $text_color = imagecolorallocate($img, mt_rand(30, 180), mt_rand(10, 100), mt_rand(40, 250));
  23. imagechar($img, 5, $x + 5, $y + 3, $randCode[$i], $text_color);
  24. }
  25. for ($j = 0; $j < 60; $j++) {
  26. $x = mt_rand(0, 70);
  27. $y = mt_rand(0, 22);
  28. imagesetpixel($img, $x, $y, $pixColor);
  29. }
  30. ob_clean();
  31. header('Content-Type: image/jpeg');
  32. imagejpeg($img);
  33. imagedestroy($img);
  34. }
  35. }
  36. ?>