captcha

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2021 License: MIT Imports: 27 Imported by: 11

Documentation

Overview

*

  • @Author Awen
  • @Description Captcha Config
  • @Date 2021/7/18
  • @Email wengaolng@gmail.com *

*

  • @Author Awen
  • @Description Captcha Draw
  • @Date 2021/7/18
  • @Email wengaolng@gmail.com *

*

  • @Author Awen
  • @Description Captcha Palette
  • @Date 2021/7/18
  • @Email wengaolng@gmail.com *

Index

Constants

View Source
const (
	// 无扭曲
	ThumbBackgroundDistortNone = iota
	// 扭曲程度 1-5级别
	ThumbBackgroundDistortLevel1
	ThumbBackgroundDistortLevel2
	ThumbBackgroundDistortLevel3
	ThumbBackgroundDistortLevel4
	ThumbBackgroundDistortLevel5
)

*

  • @Description: 扭曲程度

Variables

This section is empty.

Functions

func Child

func Child(path string) []string

*

  • @Description: 获取路径
  • @param path
  • @return []string

func EncodeB64string

func EncodeB64string(img image.Image) string

*

  • @Description: base64编码
  • @param img
  • @return string

func GenerateKey

func GenerateKey(str string) (string, error)

*

  • @Description: 生成KEY
  • @param str str
  • @return Hash
  • @return err

func GetCaptchaDefaultChars

func GetCaptchaDefaultChars() []string

*

  • @Description: 获取字符
  • @return map[int]string

func HexToRgb

func HexToRgb(hex string) (int64, int64, int64)

*

  • @Description: 十六进制转RBG颜色
  • @param hex
  • @return int64
  • @return int64
  • @return int64

func InArrayWithStr

func InArrayWithStr(items []string, s string) bool

*

  • @Description: InArrayS 如果 s 在 items 中,返回 true;否则,返回 false。
  • @param items
  • @param s
  • @return bool

func IsChineseChar

func IsChineseChar(str string) bool

*

  • @Description: 判断是否是中文
  • @param str
  • @return bool

func LenChineseChar

func LenChineseChar(str string) int

*

  • @Description: 计算中文及字母长度,例如:“你好hello” = 7
  • @param str
  • @return int

func Md5ToString

func Md5ToString(str string) string

*

  • @Description: Md5
  • @param str
  • @return string

func ParseHexColor

func ParseHexColor(s string) (c color.RGBA, err error)

*

  • @Description: 把十六进制颜色转 color.RGBA
  • @param v
  • @return out
  • @return err

func PathExists

func PathExists(path string) (bool, error)

*

  • @Description: 检测文件是否存在
  • @param path
  • @return bool
  • @return error

func RandFloat

func RandFloat(min, max int) float64

*

  • @Description: 随机浮点数
  • @param min
  • @param max
  • @return float64

func RandInt

func RandInt(min, max int) (result int)

*

  • @Description: 生成区间[-m, n]的安全随机数
  • @param min
  • @param max
  • @return int

func RgbToHex

func RgbToHex(red int64, green int64, blue int64) string

*

  • @Description: RBG颜色转十六进制颜色
  • @param red
  • @param green
  • @param blue
  • @return CaptchaColorHEX

Types

type AreaPoint

type AreaPoint struct {
	MinX, MaxX, MinY, MaxY int
}

*

  • @Description: 区域点信息

type Captcha

type Captcha struct {
	// 字符集合,用于随机字符串
	Chars []string
	// 难度码配置
	Config *Config
	// 验证画图
	CaptchaDraw *Draw
}

*

  • @Description: 点选验证码

func GetCaptcha

func GetCaptcha() *Captcha

*

  • @Description: 获取点选验证码
  • @return *Captcha

func NewCaptcha

func NewCaptcha() *Captcha

*

  • @Description: 创建点选验证码
  • @return *Captcha

func (*Captcha) EncodeB64string

func (cc *Captcha) EncodeB64string(img image.Image) string

*

  • @Description: base64编码
  • @receiver cc
  • @param img
  • @return string

func (*Captcha) Generate

func (cc *Captcha) Generate() (map[int]CharDot, string, string, string, error)

*

  • @Description: 根据设置的尺寸生成验证码图片
  • @return CaptchaCharDot 位置信息
  • @return string 主图Base64
  • @return string 缩略图Base64
  • @return error

func (*Captcha) GenerateWithSize

func (cc *Captcha) GenerateWithSize(imageSize *Size, thumbnailSize *Size) (map[int]CharDot, string, string, string, error)

*

  • @Description: 生成验证码图片
  • @param imageSize 主图尺寸
  • @param thumbnailSize 缩略图尺寸
  • @return CaptchaCharDot 位置信息
  • @return string 主图Base64
  • @return string 缩略图Base64
  • @return error

func (*Captcha) SetBackground

func (cc *Captcha) SetBackground(images []string)

*

  • @Description: 设置随机背景图片
  • @receiver cc
  • @param colors

func (*Captcha) SetFont

func (cc *Captcha) SetFont(fonts []string)

*

  • @Description: 设置随机字体
  • @receiver cc
  • @param colors

func (*Captcha) SetImageFontAlpha

func (cc *Captcha) SetImageFontAlpha(val float64)

*

  • @Description: 设置验证码文本透明度
  • @receiver cc
  • @param val

func (*Captcha) SetImageFontDistort

func (cc *Captcha) SetImageFontDistort(val int)

*

  • @Description: 设置验证码文本扭曲程度
  • @receiver cc
  • @param val

func (*Captcha) SetImageSize

func (cc *Captcha) SetImageSize(size *Size)

*

  • @Description: 设置验证码尺寸
  • @receiver cc
  • @param size

func (*Captcha) SetRangChars

func (cc *Captcha) SetRangChars(chars []string) error

*

  • @Description: 设置随机字符串,每个单词不能超出2个字符,超出会影响位置的验证
  • @receiver cc
  • @param chars
  • @return error

func (*Captcha) SetTextRangAnglePos

func (cc *Captcha) SetTextRangAnglePos(pos []*RangeVal)

*

  • @Description: 设置文本角度位置
  • @receiver cc
  • @param pos

func (*Captcha) SetTextRangFontColors

func (cc *Captcha) SetTextRangFontColors(colors []string)

*

  • @Description: 设置文本随机颜色
  • @receiver cc
  • @param colors

func (*Captcha) SetTextRangLen

func (cc *Captcha) SetTextRangLen(val *RangeVal)

*

  • @Description: 设置字符随机长度范围
  • @receiver cc
  • @param val

func (*Captcha) SetThumbBackground

func (cc *Captcha) SetThumbBackground(images []string)

*

  • @Description: 设置随机背景图片
  • @receiver cc
  • @param colors

func (*Captcha) SetThumbBgCirclesNum

func (cc *Captcha) SetThumbBgCirclesNum(val int)

*

  • @Description: 设置缩略图小圆点数量
  • @receiver cc
  • @param val

func (*Captcha) SetThumbBgColors

func (cc *Captcha) SetThumbBgColors(colors []string)

*

  • @Description: 设置缩略图随机背景颜色
  • @receiver cc
  • @param colors

func (*Captcha) SetThumbBgDistort

func (cc *Captcha) SetThumbBgDistort(val int)

*

  • @Description: 设置缩略图扭曲程度
  • @receiver cc
  • @param val

func (*Captcha) SetThumbBgSlimLineNum

func (cc *Captcha) SetThumbBgSlimLineNum(val int)

*

  • @Description: 缩略图线条数量
  • @receiver cc
  • @param val

func (*Captcha) SetThumbSize

func (cc *Captcha) SetThumbSize(size *Size)

*

  • @Description: 设置缩略图尺寸
  • @receiver cc
  • @param size

type CharDot

type CharDot struct {
	// x,y位置
	Dx int
	Dy int
	// 字体大小
	Size int
	// 字体宽
	Width int
	// 字体高
	Height int
	// 字符文本
	Text string
	// 字体角度
	Angle int
	// 颜色
	Color string
}

*

  • @Description: 图片点数据

type Config

type Config struct {
	// 随机字符串长度范围
	RangTextLen *RangeVal
	// 随机验证字符串长度范围, 注意:RangCheckTextLen < RangTextLen
	RangCheckTextLen *RangeVal
	// 随机文本角度范围集合
	RangTexAnglePos []*RangeVal
	// 随机文本尺寸范围集合
	RangFontSize *RangeVal
	// 随机缩略文本尺寸范围集合
	RangCheckFontSize *RangeVal
	// 随机文本颜色	格式:"#541245"
	RangFontColors []string
	// 随机字体	格式:字体绝对路径字符串, /home/..../xxx.ttf
	RangFont []string
	// 屏幕每英寸的分辨率
	FontDPI int
	// 随机验证码背景图		格式:图片绝对路径字符串, /home/..../xxx.png
	RangBackground []string
	// 验证码尺寸, 注意:高度 > RangFontSize.max , 长度 > RangFontSize.max * RangFontSize.max
	ImageSize *Size
	// 验证码文本扭曲程度
	ImageFontDistort int
	// 验证码文本透明度 0-1
	ImageFontAlpha float64
	// 缩略图尺寸, 注意:高度 > RangCheckFontSize.max , 长度 > RangCheckFontSize.max * RangFontSize.max
	ThumbnailSize *Size
	// 随机缩略背景图		格式:图片绝对路径字符串, /home/..../xxx.png
	RangThumbBackground []string
	// 缩略图背景随机色	格式:"#541245"
	RangThumbBgColors []string
	// 缩略图扭曲程度,值为 ThumbBackgroundDistort...,
	ThumbBgDistort int
	// 缩略图小圆点数量
	ThumbBgCirclesNum int
	// 缩略图线条数量
	ThumbBgSlimLineNum int
}

* * @Description: 验证码配置

func GetCaptchaDefaultConfig

func GetCaptchaDefaultConfig() *Config

*

  • @Description: 获取默认配置
  • @return CaptchaConfig

type Draw

type Draw struct{}

*

  • @Description: 验证码画图

func (*Draw) CreateCanvas

func (cd *Draw) CreateCanvas(params *DrawCanvas, isAlpha bool) *image.NRGBA

*

  • @Description: 创建 NRGBA 画布
  • @receiver cd
  • @param params
  • @param isAlpha
  • @return *image.NRGBA

func (*Draw) CreateCanvasWithPalette

func (cd *Draw) CreateCanvasWithPalette(params *DrawCanvas, colorArr []color.RGBA) *Palette

*

  • @Description: 创建 Palette 带调包板画布
  • @receiver cd
  • @param params
  • @param colorArr
  • @return *Palette

func (*Draw) Draw

func (cd *Draw) Draw(params *DrawCanvas) (image.Image, error)

*

  • @Description: 画图
  • @receiver cd
  • @param params
  • @return image.Image
  • @return error

func (*Draw) DrawTextImg

func (cd *Draw) DrawTextImg(dot *DrawDot, params *DrawCanvas) (*Palette, *AreaPoint, error)

*

  • @Description: 根据
  • @receiver cd
  • @param dot
  • @param params
  • @return *Palette
  • @return *AreaPoint
  • @return error

func (*Draw) DrawWithPalette

func (cd *Draw) DrawWithPalette(params *DrawCanvas, colorA []color.Color, colorB []color.Color) (image.Image, error)

*

  • @Description: 使用调色板的画布画图
  • @receiver cd
  • @param params
  • @param colorA
  • @param colorB
  • @return image.Image
  • @return error

type DrawCanvas

type DrawCanvas struct {
	// 长、高
	Width  int
	Height int
	// 背景图片
	Background string
	// 缩略图扭曲程度,值为 ThumbBackgroundDistort...,
	BackgroundDistort int
	// 缩略图小圆点数量
	BackgroundCirclesNum int
	// 缩略图线条数量
	BackgroundSlimLineNum int
	// 文本透明度
	TextAlpha float64

	CaptchaDrawDot []*DrawDot
}

*

  • @Description: 验证码画图

type DrawDot

type DrawDot struct {
	Dx      int
	Dy      int
	FontDPI int
	Text    string
	Size    int
	Width   int
	Height  int
	Angle   int
	Color   string
	Font    string
}

type Palette

type Palette struct {
	*image.Paletted
}

*

  • @Description: 调色板

func NewPalette

func NewPalette(r image.Rectangle, p color.Palette) *Palette

*

  • @Description: 创建调色板
  • @param r
  • @param p
  • @return *Palette

func (*Palette) Rotate

func (p *Palette) Rotate(angle int)

*

  • @Description: 旋转任意角度
  • @receiver p
  • @param angle

type Point

type Point struct {
	X int
	Y int
}

*

  • @Description: 点

type RangeVal

type RangeVal struct {
	Min int
	Max int
}

*

  • @Description: 范围值
  • @Example: {min: 0, max: 45} 从0-45中取任意值

type Size

type Size struct {
	Width  int
	Height int
}

*

  • @Description: 尺寸
  • @Example: {width: 0, height: 45} 从0-45中取任意值

Jump to

Keyboard shortcuts

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