Captcha.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * 验证码
  4. */
  5. namespace app\api\controller\v2;
  6. use app\common\controller\Base;
  7. class Captcha extends Base
  8. {
  9. public function getCaptcha()
  10. {
  11. $rand = input('random');
  12. $config = [
  13. // 验证码字体大小
  14. 'fontSize' => 30,
  15. // 验证码位数
  16. 'length' => 5,
  17. // 关闭验证码杂点
  18. 'useNoise' => false,
  19. ];
  20. $captcha = new \app\common\library\Captcha($config);
  21. log_message('Captcha.getCaptcha: random=' . $rand, 'log', LOG_PATH . 'apilog/');
  22. // 便于测试
  23. if($rand == 'eb55f905cbd4220b3c06b0e995a441d41_wen'){
  24. return json_encode([
  25. 'code' =>0,
  26. 'msg' => 'success',
  27. 'time' => request()->server('REQUEST_TIME'),
  28. 'data' => $captcha->entryJson($rand),
  29. ]);
  30. }
  31. $img = $captcha->entry($rand);
  32. // type = 方便测试返回json用的
  33. if(input('type', 0) == 1){
  34. return json_encode(['code' =>1, 'msg'=>'success', 'data' => ['img_str' => "data:image/png;base64,".base64_encode($img->getContent()), 'random' => $rand]]);
  35. }
  36. return $img;
  37. }
  38. public function vCaptcha()
  39. {
  40. $captcha = new \app\common\library\Captcha();
  41. var_dump($captcha->check(input('code'), input('random')));
  42. }
  43. }