c7a

package
v0.0.0-...-561b508 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 11 Imported by: 0

README

Go生成图形验证码

Go生成图形验证码,带噪点,干扰线,正弦干扰线;支持生成普通的字符串和简单的数学算术运算公式,

source fork from here, font from lifei6671/gocaptcha

Example

package main

import (
	"github.com/bingoohuang/gg-rand/pkg/c7a"
	"image/png"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
		cp := c7a.NewCaptcha(150, 40, 5)
		cp.SetMode(c7a.SimpleMathFormula) // 设置为数学公式
		code, img := cp.OutPut()
		// 备注:code 可以根据情况存储到session,并在使用时取出验证

		log.Printf("Captcha: %s", code)
		w.Header().Set("Content-Type", "image/png; charset=utf-8")
		png.Encode(w, img)
	})

	if err := http.ListenAndServe("localhost:1200", nil); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FontDENNEthreedee []byte

Functions

This section is empty.

Types

type Captcha

type Captcha struct {
	W, H, CodeLen int
	FontSize      float64
	Dpi           int
	// contains filtered or unexported fields
}

Captcha 图形验证码 使用字体默认ttf格式 w 图片宽度, h图片高度,CodeLen验证码的个数 FontSize 字体大小, Dpi 清晰度 mode 验证模式

func NewCaptcha

func NewCaptcha(w, h, CodeLen int) *Captcha

NewCaptcha 实例化验证码

func (*Captcha) OutPut

func (c *Captcha) OutPut() (string, *image.RGBA)

OutPut 输出

func (*Captcha) RangeRand

func (c *Captcha) RangeRand(min, max int64) int64

RangeRand 获取区间[-m, n]的随机数

func (*Captcha) SetFontSize

func (c *Captcha) SetFontSize(fontSize float64)

SetFontSize 设置字体大小

func (*Captcha) SetMode

func (c *Captcha) SetMode(mode Mode)

SetMode 设置模式

type Mode

type Mode int
const (
	DirectString      Mode = iota // 普通字符串
	SimpleMathFormula             // 10以内简单数学公式
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL