CaptchaController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace cmf\controller;
  12. use think\captcha\Captcha;
  13. use think\Request;
  14. class CaptchaController
  15. {
  16. /**
  17. * captcha/new?height=50&width=200&font_size=25&length=4&bg=243,251,254&id=1
  18. * @param Request $request
  19. * @return \think\Response
  20. */
  21. public function index(Request $request)
  22. {
  23. $config = [
  24. // 验证码字体大小(px)
  25. 'fontSize' => 25,
  26. // 验证码字体大小(px)
  27. 'useCurve' => true,
  28. // 是否画混淆曲线
  29. 'useNoise' => true,
  30. // 验证码图片高度
  31. 'imageH' => 0,
  32. // 验证码图片宽度
  33. 'imageW' => 0,
  34. // 验证码位数
  35. 'length' => 4,
  36. // 背景颜色
  37. 'bg' => [243, 251, 254],
  38. ];
  39. $fontSize = $request->param('font_size', 25, 'intval');
  40. if ($fontSize > 8) {
  41. $config['fontSize'] = $fontSize;
  42. }
  43. $imageH = $request->param('height', '');
  44. if ($imageH != '') {
  45. $config['imageH'] = intval($imageH);
  46. }
  47. $imageW = $request->param('width', '');
  48. if ($imageW != '') {
  49. $config['imageW'] = intval($imageW);
  50. }
  51. $length = $request->param('length', 4, 'intval');
  52. if ($length > 2) {
  53. $config['length'] = $length;
  54. }
  55. $bg = $request->param('bg', '');
  56. if (!empty($bg)) {
  57. $bg = explode(',', $bg);
  58. array_walk($bg, 'intval');
  59. if (count($bg) > 2 && $bg[0] < 256 && $bg[1] < 256 && $bg[2] < 256) {
  60. $config['bg'] = $bg;
  61. }
  62. }
  63. $id = $request->param('id', 0, 'intval');
  64. if ($id > 5 || empty($id)) {
  65. $id = '';
  66. }
  67. $defaultCaptchaConfig = config('captcha');
  68. if ($defaultCaptchaConfig && is_array($defaultCaptchaConfig)) {
  69. $config = array_merge($defaultCaptchaConfig, $config);
  70. }
  71. $captcha = new Captcha($config);
  72. @ob_clean();// 清除输出缓存
  73. return $captcha->entry($id);
  74. }
  75. }