Config.php

生成验证码前需要传入Config的对象实例
Config类实例化后会有默认配置,无需配置也可生成二维码

实现代码:

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | easySwoole [ use swoole easily just like echo "hello world" ]
  4. // +----------------------------------------------------------------------
  5. // | WebSite: https://www.easyswoole.com
  6. // +----------------------------------------------------------------------
  7. // | Welcome Join QQGroup 633921431
  8. // +----------------------------------------------------------------------
  9. namespace EasySwoole\VerifyCode;
  10. use EasySwoole\Spl\SplBean;
  11. /**
  12. * 验证码配置文件
  13. * Class VerifyCodeConf
  14. * @author : evalor <master@evalor.cn>
  15. * @package Vendor\VerifyCode
  16. */
  17. class Conf extends SplBean
  18. {
  19. public $charset = '1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'; // 字母表
  20. public $useCurve = false; // 混淆曲线
  21. public $useNoise = false; // 随机噪点
  22. public $useFont = null; // 指定字体
  23. public $fontColor = null; // 字体颜色
  24. public $backColor = null; // 背景颜色
  25. public $imageL = null; // 图片宽度
  26. public $imageH = null; // 图片高度
  27. public $fonts = []; // 额外字体
  28. public $fontSize = 25; // 字体大小
  29. public $length = 4; // 生成位数
  30. public $mime = MIME::PNG; // 设置类型
  31. public $temp = '/tmp'; // 设置缓存目录
  32. public function setTemp($temp){
  33. if (!is_dir($temp)) mkdir($temp,0755) && chmod($temp,0755);
  34. $this->temp = $temp;
  35. }
  36. /**
  37. * 设置图片格式
  38. * @param $MimeType
  39. * @author : evalor <master@evalor.cn>
  40. * @return Conf
  41. */
  42. public function setMimeType($MimeType)
  43. {
  44. $allowMime = [ MIME::PNG, MIME::GIF, MIME::JPG ];
  45. if (in_array($MimeType, $allowMime)) $this->mime = $MimeType;
  46. return $this;
  47. }
  48. /**
  49. * 设置字符集
  50. * @param string $charset
  51. * @return Conf
  52. */
  53. public function setCharset($charset)
  54. {
  55. is_string($charset) && $this->charset = $charset;
  56. return $this;
  57. }
  58. /**
  59. * 开启混淆曲线
  60. * @param bool $useCurve
  61. * @return Conf
  62. */
  63. public function setUseCurve($useCurve = true)
  64. {
  65. is_bool($useCurve) && $this->useCurve = $useCurve;
  66. return $this;
  67. }
  68. /**
  69. * 开启噪点生成
  70. * @param bool $useNoise
  71. * @return Conf
  72. */
  73. public function setUseNoise($useNoise = true)
  74. {
  75. is_bool($useNoise) && $this->useNoise = $useNoise;
  76. return $this;
  77. }
  78. /**
  79. * 使用自定义字体
  80. * @param string $useFont
  81. * @return Conf
  82. */
  83. public function setUseFont($useFont)
  84. {
  85. is_string($useFont) && $this->useFont = $useFont;
  86. return $this;
  87. }
  88. /**
  89. * 设置文字颜色
  90. * @param array|string $fontColor
  91. * @return Conf
  92. */
  93. public function setFontColor($fontColor)
  94. {
  95. if (is_string($fontColor)) $this->fontColor = $this->HEXToRGB($fontColor);
  96. if (is_array($fontColor)) $this->fontColor = $fontColor;
  97. return $this;
  98. }
  99. /**
  100. * 设置背景颜色
  101. * @param null $backColor
  102. * @return Conf
  103. */
  104. public function setBackColor($backColor)
  105. {
  106. if (is_string($backColor)) $this->backColor = $this->HEXToRGB($backColor);
  107. if (is_array($backColor)) $this->backColor = $backColor;
  108. return $this;
  109. }
  110. /**
  111. * 设置图片宽度
  112. * @param int|string $imageL
  113. * @return Conf
  114. */
  115. public function setImageWidth($imageL)
  116. {
  117. $this->imageL = intval($imageL);
  118. return $this;
  119. }
  120. /**
  121. * 设置图片高度
  122. * @param null $imageH
  123. * @return Conf
  124. */
  125. public function setImageHeight($imageH)
  126. {
  127. $this->imageH = intval($imageH);
  128. return $this;
  129. }
  130. /**
  131. * 设置字体集
  132. * @param array|string $fonts
  133. * @return Conf
  134. */
  135. public function setFonts($fonts)
  136. {
  137. if (is_string($fonts)) array_push($this->fonts, $fonts);
  138. if (is_array($fonts) && !empty($fonts)) {
  139. if (empty($this->fonts)) {
  140. $this->fonts = $fonts;
  141. } else {
  142. array_merge($this->fonts, $fonts);
  143. }
  144. }
  145. return $this;
  146. }
  147. /**
  148. * 设置字体尺寸
  149. * @param int $fontSize
  150. * @return Conf
  151. */
  152. public function setFontSize($fontSize)
  153. {
  154. $this->fontSize = intval($fontSize);
  155. return $this;
  156. }
  157. /**
  158. * 设置验证码长度
  159. * @param int $length
  160. * @return Conf
  161. */
  162. public function setLength($length)
  163. {
  164. $this->length = intval($length);
  165. return $this;
  166. }
  167. /**
  168. * 获取配置值
  169. * @param $name
  170. * @author : evalor <master@evalor.cn>
  171. * @return mixed
  172. */
  173. public function __get($name)
  174. {
  175. return $this->$name;
  176. }
  177. /**
  178. * 十六进制转RGB
  179. * @param $hexColor
  180. * @author : evalor <master@evalor.cn>
  181. * @return array
  182. */
  183. function HEXToRGB($hexColor)
  184. {
  185. $color = str_replace('#', '', $hexColor);
  186. if (strlen($color) > 3) {
  187. $rgb = array(
  188. hexdec(substr($color, 0, 2)),
  189. hexdec(substr($color, 2, 2)),
  190. hexdec(substr($color, 4, 2))
  191. );
  192. } else {
  193. $color = $hexColor;
  194. $r = substr($color, 0, 1) . substr($color, 0, 1);
  195. $g = substr($color, 1, 1) . substr($color, 1, 1);
  196. $b = substr($color, 2, 1) . substr($color, 2, 1);
  197. $rgb = array(
  198. hexdec($r),
  199. hexdec($g),
  200. hexdec($b)
  201. );
  202. }
  203. return $rgb;
  204. }
  205. }