gocaptcha

package module
v0.0.0-...-c6afb47 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2018 License: Apache-2.0 Imports: 20 Imported by: 0

README

gocaptcha

一个简单的Go语言实现的验证码

图片实例

image image image image

简介

基于Golang实现的图片验证码生成库,可以实现随机字母个数,随机直线,随机噪点等。可以设置任意多字体,每个验证码随机选一种字体展示。

实例

使用:
	go get github.com/lifei6671/gocaptcha/
使用的类库
	go get github.com/golang/freetype
	go get github.com/golang/freetype/truetype
	go get golang.org/x/image

天朝可以去 http://www.golangtc.com/download/packagehttps://gopm.io 下载

代码

具体实例可以查看example目录,有生成的验证码图片。

	
  func Get(w http.ResponseWriter, r *http.Request) {
      //初始化一个验证码对象
		captchaImage,err := gocaptcha.NewCaptchaImage(dx,dy,gocaptcha.RandLightColor());

  	  //画上三条随机直线
  	  captchaImage.Drawline(3);

  	  //画边框
  	  captchaImage.DrawBorder(gocaptcha.ColorToRGB(0x17A7A7A));
      
  	  //画随机噪点
  	  captchaImage.DrawNoise(gocaptcha.CaptchaComplexHigh);
  
  	  //画随机文字噪点
  	  captchaImage.DrawTextNoise(gocaptcha.CaptchaComplexLower);
      //画验证码文字,可以预先保持到Session种或其他储存容器种
  	  captchaImage.DrawText(gocaptcha.RandText(4));
    	if err != nil {
    		  fmt.Println(err)
    	}
  	  //将验证码保持到输出流种,可以是文件或HTTP流等
		  captchaImage.SaveImage(w,gocaptcha.ImageFormatJpeg);
	}

Documentation

Index

Constants

View Source
const (
	//图片格式
	ImageFormatPng = iota
	ImageFormatJpeg
	ImageFormatGif
	//验证码噪点强度
	CaptchaComplexLower = iota
	CaptchaComplexMedium
	CaptchaComplexHigh
)

Variables

View Source
var (
	FontFamily []string = make([]string, 0)
)

Functions

func ColorToRGB

func ColorToRGB(colorVal int) color.RGBA

颜色代码转换为RGB input int output int red, green, blue.

func RandColor

func RandColor() color.RGBA

生成随机颜色.

func RandDeepColor

func RandDeepColor() color.RGBA

随机生成深色系.

func RandFontFamily

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

获取所及字体.

func RandLightColor

func RandLightColor() color.RGBA

随机生成浅色.

func RandText

func RandText(num int) string

生成随机字体.

func Random

func Random(min int64, max int64) float64

生成指定大小的随机数.

func ReadFonts

func ReadFonts(dirPth string, suffix string) (err error)

获取指定目录下的所有文件,不进入下一级目录搜索,可以匹配后缀过滤。

func SetFontFamily

func SetFontFamily(fontPath ...string)

添加一个字体路径到字体库.

Types

type CaptchaImage

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

func NewCaptchaImage

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

新建一个图片对象

func (*CaptchaImage) DrawBorder

func (captcha *CaptchaImage) DrawBorder(borderColor color.RGBA) *CaptchaImage

画边框.

func (*CaptchaImage) DrawHollowLine

func (captcha *CaptchaImage) DrawHollowLine() *CaptchaImage

添加一个较粗的空白直线

func (*CaptchaImage) DrawNoise

func (captcha *CaptchaImage) DrawNoise(complex int) *CaptchaImage

画噪点.

func (*CaptchaImage) DrawSineLine

func (captcha *CaptchaImage) DrawSineLine() *CaptchaImage

func (*CaptchaImage) DrawText

func (captcha *CaptchaImage) DrawText(text string) error

写字.

func (*CaptchaImage) DrawTextNoise

func (captcha *CaptchaImage) DrawTextNoise(complex int) error

画文字噪点.

func (*CaptchaImage) Drawline

func (captcha *CaptchaImage) Drawline(num int) *CaptchaImage

画一条直线

func (*CaptchaImage) SaveImage

func (this *CaptchaImage) SaveImage(w io.Writer, imageFormat int) error

保存图片对象

type Point

type Point struct {
	X int
	Y int
}

func NewPoint

func NewPoint(x int, y int) *Point

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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