1 Star 0 Fork 45

木树/go验证码合集包

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
fontmanager.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
jufakeji 提交于 2019-07-07 16:16 +08:00 . commit
//++++++++++++++++++++++++++++++++++++++++
//Fighting for great,share generate value!
//Build the best soft by golang,let's go!
//++++++++++++++++++++++++++++++++++++++++
//Author:ShirDon <http://www.shirdon.com>
//Email:hcbsts@163.com; 823923263@qq.com
//++++++++++++++++++++++++++++++++++++++++
package captchas_with_go
import (
"github.com/golang/freetype/truetype"
"io/ioutil"
"math/rand"
"time"
)
//FontManager is a Font Manager the manage font list
type FontManager struct {
fontFiles []string
fontObjects map[string]*truetype.Font
randObject *rand.Rand
}
//CreateFontManager will create a new Font Manager
func CreateFontManager() *FontManager {
fm := new(FontManager)
fm.fontFiles = []string{}
fm.fontObjects = make(map[string]*truetype.Font)
fm.randObject = rand.New(rand.NewSource(time.Now().UnixNano()))
return fm
}
//AddFont will add a new font file to the list
func (fm *FontManager) AddFont(pathToFontFile string) error {
fontBytes, err := ioutil.ReadFile(pathToFontFile)
if err != nil {
return err
}
font, err := truetype.Parse(fontBytes)
if err != nil {
return err
}
fm.fontFiles = append(fm.fontFiles, pathToFontFile)
fm.fontObjects[pathToFontFile] = font
return nil
}
//GetFont will return a Font struct by path
func (fm *FontManager) GetFont(pathToFontFile string) *truetype.Font {
return fm.fontObjects[pathToFontFile]
}
//GetRandomFont will return a random Font struct
func (fm *FontManager) GetRandomFont() *truetype.Font {
randomIndex := fm.randObject.Intn(len(fm.fontFiles))
fontFile := fm.fontFiles[randomIndex]
rst := fm.GetFont(fontFile)
return rst
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mushu/captchas_with_go.git
git@gitee.com:mushu/captchas_with_go.git
mushu
captchas_with_go
go验证码合集包
master

搜索帮助