验证码

Testing Is Documentation

tests/Seccode/SeccodeTest.php验证码 - 图1

QueryPHP 提供的验证组件,扩展包内定义了一些常见用法方便使用,可以满足大部分常用场景。

配置

验证码带有默认的配置参数,支持自定义配置。

参数默认值描述
width160验证码宽度
height60验证码高度
tilttrue随机倾斜度
colortrue随机颜色
sizetrue随机大小
font_path英文字体路径
chinese_font_path中文字体路径

Uses

  1. <?php
  2. use Leevel\Seccode\Seccode;

display 验证码基本使用

  1. public function testBaseUse(): void
  2. {
  3. $seccode = new Seccode([
  4. 'font_path' => __DIR__.'/font',
  5. ]);
  6. $file = __DIR__.'/baseuse.png';
  7. $seccode->display('abc', $file);
  8. $this->assertTrue(is_file($file));
  9. $info = getimagesize($file);
  10. $data = <<<'eot'
  11. {
  12. "0": 160,
  13. "1": 60,
  14. "2": 3,
  15. "3": "width=\"160\" height=\"60\"",
  16. "bits": 8,
  17. "mime": "image\/png"
  18. }
  19. eot;
  20. $this->assertSame(
  21. $data,
  22. $this->varJson(
  23. $info
  24. )
  25. );
  26. unlink($file);
  27. }

验证码支持中文

  1. public function testChinese(): void
  2. {
  3. $seccode = new Seccode([
  4. 'font_path' => __DIR__.'/font',
  5. 'chinese_font_path' => __DIR__.'/chinese', // 中文字体过于庞大,本地已经测试通过,这里用的英文的假字体,会乱码
  6. ]);
  7. $file = __DIR__.'/chinese.png';
  8. $seccode->display('中国', $file);
  9. $this->assertTrue(is_file($file));
  10. $info = getimagesize($file);
  11. $data = <<<'eot'
  12. {
  13. "0": 160,
  14. "1": 60,
  15. "2": 3,
  16. "3": "width=\"160\" height=\"60\"",
  17. "bits": 8,
  18. "mime": "image\/png"
  19. }
  20. eot;
  21. $this->assertSame(
  22. $data,
  23. $this->varJson(
  24. $info
  25. )
  26. );
  27. unlink($file);
  28. }

验证码支持随机生成

支持的类型

  1. # Tests\Seccode\SeccodeTest::getAutoCodeData
  2. public function getAutoCodeData(): array
  3. {
  4. return [
  5. [Seccode::ALPHA_NUM],
  6. [Seccode::ALPHA_NUM_LOWERCASE],
  7. [Seccode::ALPHA_NUM_UPPERCASE],
  8. [Seccode::ALPHA],
  9. [Seccode::ALPHA_LOWERCASE],
  10. [Seccode::ALPHA_UPPERCASE],
  11. [Seccode::NUM],
  12. [Seccode::CHINESE],
  13. ];
  14. }
  1. public function testAutoCode(string $type): void
  2. {
  3. $seccode = new Seccode([
  4. 'font_path' => __DIR__.'/font',
  5. 'chinese_font_path' => __DIR__.'/chinese', // 中文字体过于庞大,本地已经测试通过,这里用的英文的假字体,会乱码
  6. ]);
  7. $file = __DIR__.'/autocode.'.$type.'.png';
  8. $seccode->display(4, $file, true, $type);
  9. $this->assertTrue(is_file($file));
  10. $info = getimagesize($file);
  11. $data = <<<'eot'
  12. {
  13. "0": 160,
  14. "1": 60,
  15. "2": 3,
  16. "3": "width=\"160\" height=\"60\"",
  17. "bits": 8,
  18. "mime": "image\/png"
  19. }
  20. eot;
  21. $this->assertSame(
  22. $data,
  23. $this->varJson(
  24. $info
  25. )
  26. );
  27. unlink($file);
  28. }

验证码支持最小尺寸设置

  1. public function testMinWidthAndMinHeight(): void
  2. {
  3. $seccode = new Seccode([
  4. 'font_path' => __DIR__.'/font',
  5. 'width' => 2,
  6. 'height' => 2,
  7. ]);
  8. $file = __DIR__.'/minWidthAndMinHeight.png';
  9. $seccode->display('A', $file);
  10. $this->assertTrue(is_file($file));
  11. $info = getimagesize($file);
  12. $data = <<<'eot'
  13. {
  14. "0": 16,
  15. "1": 16,
  16. "2": 3,
  17. "3": "width=\"16\" height=\"16\"",
  18. "bits": 8,
  19. "mime": "image\/png"
  20. }
  21. eot;
  22. $this->assertSame(
  23. $data,
  24. $this->varJson(
  25. $info
  26. )
  27. );
  28. unlink($file);
  29. }

验证码支持最大尺寸设置

  1. public function testMaxWidthAndMaxHeight(): void
  2. {
  3. $seccode = new Seccode([
  4. 'font_path' => __DIR__.'/font',
  5. 'width' => 1200,
  6. 'height' => 1200,
  7. ]);
  8. $file = __DIR__.'/maxWidthAndMaxHeight.png';
  9. $seccode->display('IMAX', $file);
  10. $this->assertTrue(is_file($file));
  11. $info = getimagesize($file);
  12. $data = <<<'eot'
  13. {
  14. "0": 999,
  15. "1": 999,
  16. "2": 3,
  17. "3": "width=\"999\" height=\"999\"",
  18. "bits": 8,
  19. "mime": "image\/png"
  20. }
  21. eot;
  22. $this->assertSame(
  23. $data,
  24. $this->varJson(
  25. $info
  26. )
  27. );
  28. unlink($file);
  29. }

验证码随机颜色

  1. public function testWithoutRandColor(): void
  2. {
  3. $seccode = new Seccode([
  4. 'font_path' => __DIR__.'/font',
  5. 'color' => false,
  6. ]);
  7. $file = __DIR__.'/withoutRandColor.png';
  8. $seccode->display('ABCD', $file);
  9. $this->assertTrue(is_file($file));
  10. $info = getimagesize($file);
  11. $data = <<<'eot'
  12. {
  13. "0": 160,
  14. "1": 60,
  15. "2": 3,
  16. "3": "width=\"160\" height=\"60\"",
  17. "bits": 8,
  18. "mime": "image\/png"
  19. }
  20. eot;
  21. $this->assertSame(
  22. $data,
  23. $this->varJson(
  24. $info
  25. )
  26. );
  27. unlink($file);
  28. }