gocaptcha

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

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

Go to latest
Published: Sep 3, 2021 License: MIT Imports: 13 Imported by: 0

README

gocaptcha

图片验证码

Language Go Reference

使用示例1

package main

import (
	"fmt"
	"image/png"
	"net/http"
	"github.com/jellycheng/gocaptcha"
)

func captchaHandler(w http.ResponseWriter, r *http.Request) {
	cp := gocaptcha.NewCaptcha(120, 40)
	cp.CodeLen = 4 // 字符串长度
	cp.SetFontPath("./font/") // 设置字体目录
	cp.SetFontName("luxisr.ttf") //字体文件名
	code, img := cp.OutPut()
	//图片验证码内容,这里补充你的代码逻辑todo
	fmt.Println(code)

	// 响应图片
	w.Header().Set("Content-Type", "image/png; charset=utf-8")
	png.Encode(w, img)
}

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

	_ = http.ListenAndServe("localhost:1234", nil)

}


gin框架集成图片验证码示例

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jellycheng/gocaptcha"
	"image/png"
)
func main()  {
	r := gin.Default()
	//设置图片验证码地址
	r.GET("/captcha", func(c *gin.Context) {
		cp := gocaptcha.NewCaptcha(120, 40)
		cp.CodeLen = 4 // 字符串长度
		cp.SetFontPath("./font/") // 设置字体目录
		cp.SetFontName("luxisr.ttf") //字体文件名
		code, img := cp.OutPut()
		//图片验证码内容,这里补充你的代码逻辑todo
		fmt.Println(code)

		// 响应图片
		c.Header("Content-Type", "image/png; charset=utf-8")
		png.Encode(c.Writer, img)
	})

	r.Run(":1234")
}

Documentation

Index

Constants

View Source
const (
	ModeStr        int = 0
	ModeArithmetic int = 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Captcha

type Captcha struct {
	W        int     // 图片宽度
	H        int     // 图片高度
	CodeLen  int     //验证码长度,默认长度4
	FontSize float64 //字体大小,默认25
	Dpi      int     //清晰度,默认72
	FontPath string  //字体目录
	FontName string  //字体名字
	Mode     int     //验证模式 0:普通字符串,1:数学公式
	Charstr  string
}

图形验证码结构体

func NewCaptcha

func NewCaptcha(w, h int) *Captcha

实例化验证码

func (*Captcha) GetRandCode

func (me *Captcha) GetRandCode() string

获取随机字符串

func (*Captcha) OutPut

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

返回输出

func (*Captcha) RangeRand

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

获取区间[min, max]的随机数

func (*Captcha) SetCharstr

func (me *Captcha) SetCharstr(charstr string)

设置字符串

func (*Captcha) SetFontName

func (me *Captcha) SetFontName(fontName string)

设置字体名称

func (*Captcha) SetFontPath

func (me *Captcha) SetFontPath(fontPath string)

设置字体路径

func (*Captcha) SetFontSize

func (me *Captcha) SetFontSize(fontSize float64)

设置字体大小

func (*Captcha) SetMode

func (me *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