mycaptcha

package module
v0.0.0-...-7be088e Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2020 License: MIT Imports: 18 Imported by: 0

README

package mycaptcha

a captcha library writen in golang

installation

go get github.com/diguacheng/mycaptcha/

examples:

LfzXlwRQ nspbq9LT Q26PYUs8

feature:
  • 支持png,jpeg,gif等多种图片格式
  • 支持base64、base64url编码
  • 使用简单,只需调用一行函数
  • 支持定制化,自定义验证码图片
usage

注意:使用前需要先导入字体库的字体,可通过函数mycaptcha.LoadFonts(fontfolder)实现。

以下具体使用可参考example文件夹

1. func GetSingleCaptcha(width, height, n int) (pth, text string)

生成png格式的验证码图片并存储,返回该图片的保存路径和验证码文本

参数:

  • width 图片长度
  • height 图片高度
  • n 验证码字符的个数
  • pth 生成的验证码图片的路径
  • text 验证码文本
2.func GetCaptchaBase64(width, height, n int) (base64Str, text string)

生成png格式的验证码图片并将其编码成base64字符串,返回该图片的base64编码和验证码文本

参数:

  • width 图片长度
  • height 图片高度
  • n 验证码字符的个数
  • base64Str 生成图片的base64编码
  • text 验证码文本

HTML内联插入图片:<img src="data:image/png;base64," +base64Str +"/">

3.定制生成验证码

func GetSingleCaptcha(width, height, n int) (pth, text string)为例:

func GetSingleCaptcha(width, height, n int) (pth, text string) {
    // 创建验证码图片
	CaptchaImage := NewCaptchaImage(width, height, n, GetRandLightColor())
    // 画噪音
	CaptchaImage.DrawNoise()
   	// 画曲线干扰线
	CaptchaImage.DrawSinLine()
    // 画圆形干扰线,参数为圆的最大半径和圆的个数
	CaptchaImage.DrawCirlce(height/3,width/height*3)
    // 画验证码文本,验证码文本在NewCaptchaImage()函数内生成
	CaptchaImage.DrawText()
    // 画圆形干扰线
	CaptchaImage.DrawCirlce(width/100,100)
    // 保存验证码 参数为保存的地址,"."代表当前文件夹,format为保存格式,0:png 1:jpeg;2:gif,默认png
	pth,err := CaptchaImage.SaveImage(".", 0)
	if err!=nil{
		return
	}
    text = CaptchaImage.GetText()
	return pth,text
}

实际上就是调用一系列函数,自主决定验证码图片的大小,验证码个数,干扰线等。

other

字体保存在文件夹fonts

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FontFamily []string

FontFamily a slice stores the all fonts path

Functions

func GetCaptchaBase64

func GetCaptchaBase64(width, height, n int) (base64Str, text string)

GetCaptchaBase64 This shortcut function contains several functions for generating captchaimage. You can directly generate a captchaimage that encoded base64(or base64url) mode and its corresponding captcha through this function

func GetFont

func GetFont(dirpath, fontName string) (*truetype.Font, error)

GetFont Returns the font by fontName

func GetRandColor

func GetRandColor() color.RGBA

GetRandColor get a random rgba color

func GetRandDeepColor

func GetRandDeepColor() color.RGBA

GetRandDeepColor get a random deep color

func GetRandFont

func GetRandFont() (*truetype.Font, error)

GetRandFont Randomly return a font

func GetRandLightColor

func GetRandLightColor() color.RGBA

GetRandLightColor Get a random light color

func GetRandText

func GetRandText(n int) string

GetRandText 生成随机字符的字符串

func GetSingleCaptcha

func GetSingleCaptcha(width, height, n int) (pth, text string)

GetSingleCaptcha This shortcut function contains several functions for generating captchaimage. You can directly generate a captchaimage and save it through this function It's return stored Image's path and Captcha text

func LoadFonts

func LoadFonts(dirpath string) (err error)

ReadFonts Read all fonts in the folder dirpath

Types

type CaptchaImage

type CaptchaImage struct {
	// contains filtered or unexported fields
}

CaptchaImage text is the captcha writed in the image

func NewCaptchaImage

func NewCaptchaImage(width, height, n int, bgColor color.RGBA) *CaptchaImage

NewCaptchaImage initialization of the captchaimage struct

func (*CaptchaImage) DrawCircle

func (c *CaptchaImage) DrawCircle(maxsize int, count int)

DrawCirlce maxsize : the max redius the one circle may have ;count: the number of circle The algorithm is parametric equations that x=r*sin(a) y=r*cos(a)

func (*CaptchaImage) DrawNoise

func (c *CaptchaImage) DrawNoise()

DrawNoise draw noise

func (*CaptchaImage) DrawSinLine

func (c *CaptchaImage) DrawSinLine()

DrawSinLine Draw a sine curve

func (*CaptchaImage) DrawText

func (c *CaptchaImage) DrawText() error

DrawText write the test on image

func (*CaptchaImage) GetText

func (c *CaptchaImage) GetText() string

GetText return CaptchaImage's text

func (*CaptchaImage) ImageToBaseb64

func (c *CaptchaImage) ImageToBaseb64(format int) string

ImageToBaseb64 encode image to base64 or base64url mode

func (*CaptchaImage) SaveImage

func (c *CaptchaImage) SaveImage(saveFolder string, imageFormat int) (pth string, err error)

SaveImage save CaptchaImage to saveFolder in png mode ,jpeg mode or gif mode and the filename is CaptchaImage.text

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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