captcha

package module
v0.0.0-...-33e818b Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2019 License: MIT Imports: 13 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go生成图形验证码

Go生成图形验证码,带噪点,干扰线,正弦干扰线;支持生成普通的字符串和简单的数学算术运算公式;注意:字体需要(.ttf)格式, 另需指定字体所在的目录

Installation

安装前请确认你的电脑上go环境已安装好,可以使用以下命令

go get -u github.com/vcqr/captcha

引用包到你的项目中

import "github.com/vcqr/captcha"

Example

package main

import (
	"fmt"
	"github.com/vcqr/captcha"
	"image/png"
	"log"
	"net/http"
)

func generateCaptchaHandler(w http.ResponseWriter, r *http.Request) {
	cp := captcha.NewCaptcha(120, 40, 4)
	cp.SetFontPath("bin/") //指定字体目录
	cp.SetFontName("free") //指定字体名字
	cp.SetMode(1) //1:设置为简单的数学算术运算公式; 其他为普通字符串
	code, img := cp.OutPut()
	//备注:code 可以根据情况存储到session,并在使用时取出验证

	fmt.Println(code)

	w.Header().Set("Content-Type", "image/png; charset=utf-8")

	png.Encode(w, img) //输出为图片
}

func main() {
	http.HandleFunc("/captcha", generateCaptchaHandler)

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

输出图片举例

exp0

exp1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Captcha

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

图形验证码 使用字体默认ttf格式 w 图片宽度, h图片高度,CodeLen验证码的个数 FontSize 字体大小, Dpi 清晰度,FontPath 字体目录, FontName 字体名字 mode 验证模式 0:普通字符串,1:10以内简单数学公式

func NewCaptcha

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

实例化验证码

func (*Captcha) OutPut

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

输出

func (*Captcha) RangeRand

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

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

func (*Captcha) SetFontName

func (captcha *Captcha) SetFontName(FontName string)

设置字体名称

func (*Captcha) SetFontPath

func (captcha *Captcha) SetFontPath(FontPath string)

设置字体路径

func (*Captcha) SetFontSize

func (captcha *Captcha) SetFontSize(fontSize float64)

设置字体大小

func (*Captcha) SetMode

func (captcha *Captcha) SetMode(mode int)

设置模式

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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