title: Verification code meta:

  • name: description content: EasySwoole verification code component, can be customized to generate QR code graphics or base64 encoding.
  • name: keywords content: swoole|swoole extension|swoole framework|easyswoole|Verification code|Swoole verification code

Configuration

An object instance that needs to be passed to Config before generating the verification code After the Config class is instantiated, there will be a default configuration, and a verification code image can be generated without configuration.

Implementation code:

  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; // Confusion curve
  21. public $useNoise = false; // Random noise
  22. public $useFont = null; // Specified font
  23. public $fontColor = null; // font color
  24. public $backColor = null; // background color
  25. public $imageL = null; // Image width
  26. public $imageH = null; // Picture height
  27. public $fonts = []; // Extra font
  28. public $fontSize = 25; // font size
  29. public $length = 4; // Number of generated bits
  30. public $mime = MIME::PNG; // Setting type
  31. public $temp = '/tmp'; // Set the cache directory
  32. public function setTemp($temp){
  33. if (!is_dir($temp)) mkdir($temp,0755) && chmod($temp,0755);
  34. $this->temp = $temp;
  35. }
  36. /**
  37. * Set image format
  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. * Set character set
  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. * Turn on the confusion curve
  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. * Turn on noise generation
  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. * Use custom fonts
  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. * Set text color
  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. * Set the background color
  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. * Set image width
  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. * Set picture height
  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. * Set font set
  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. * Set the font size
  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. * Set the verification code length
  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. * Get configuration values
  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. * Hex to 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. }