coolCaptcha

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2023 License: MIT Imports: 19 Imported by: 0

README

logo

CoolCaptcha

Info

CoolCaptcha is a graphic captcha that I think is cool.The library makes it quick and easy to generate base64 data for captcha images, and it can be configured to customize the style to match the style of the product.

languages: 简体中文 English

Case

theme_default_cool theme_default_random

theme1_cool theme1_random

theme2_cool theme2_random

Install

go get github.com/Kuari/coolCaptcha

Usage

Quiuick Start Guide

package main

import (
	"github.com/Kuari/coolCaptcha"
)

func main() {
	imageBase64Data, code, err := coolCaptcha.New().GenerateImage()
}

Custom configuration

package main

import (
	"github.com/Kuari/coolCaptcha"
)

func main() {
	options := []coolCaptcha.Options{
		coolCaptcha.SetBackgroundHexColor("#c4e1f6"),                            // set the background color of the picture
		coolCaptcha.SetFontHexColor("#312E2E"),                                  // set font color
		coolCaptcha.SetLineHexColors([]string{"#f596a1", "#fadeeb", "#f9c975"}), // to set the line color, 3 bars are randomly selected from it, so this parameter sets at least 3 values
		coolCaptcha.SetWidth(300),                                               // set the width of the image
		coolCaptcha.SetHeight(120),                                              // set the height of the image
		coolCaptcha.SetCodeType(coolCaptcha.NumericCharacters),                  // set the type of authentication characters, there are three types: UppercaseEnglishCharacters, NumericCharacters, and MixedCharacters
		coolCaptcha.SetDevMode(true),                                            // Set the development module, which is suitable for saving base64 data as an image during development, so that you can easily view the generated effect
	}

	imageBase64Data, code, err := coolCaptcha.New(options...).GenerateImage()
}

Custom verification codes

You can generate images from a captcha that you generate yourself.

package main

import (
	"github.com/Kuari/coolCaptcha"
)

func main() {
	// The customCode method only supports 4-character English and numbers,
	// when passing in English, it will be capitalized and then used, so when using custom characters, the output code is capitalized, please pay attention when verifying
	// all capitalization is intended to reduce the ambiguity of English letters and numbers
	imageBase64Data, code, err := coolCaptcha.New().CustomCode("cool").GenerateImage()
}

Q&A

1. Can the default font be free for commercial use?

The default font uses blowbrush, which is free for both personal and commercial use.

And custom font settings will be opened in the future, so please stay tuned.

2. Can OCR be cracked?

CoolCaptcha has the risk of being cracked by OCR, please use it in combination with specific scenarios.

3. How to avoid the confusion between English letters and numbers?

CoolCaptcha capitalizes all English, so it is clear between English letters and numbers. You can also use SetCodeType to set the authentication character to be pure English characters or pure numbers.

4. Why was this project developed?

In some specific scenarios, graphics captcha is required, but several problems are found. First of all, most of the current captcha is similar with each other, I think the user experience is very important, designers and front-end hard work products, it will be strange to have a different style of graphic captcha. Secondly, Go's graphics captcha library is a bit small, and I worked with a Java development engineer before, and I envy the ability to quickly output a graphics captcha. Therefore, it is engaged.

Documentation

Index

Constants

View Source
const (
	UppercaseEnglishCharacters = "uppercaseEnglishCharacters"
	NumericCharacters          = "numericCharacters"
	MixedCharacters            = "mixedCharacters"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Width              int
	Height             int
	BackgroundHexColor string
	FontHexColor       string
	LineHexColors      []string
	Code               string
	CodeType           string
	DevMode            bool
}

func New

func New(options ...Options) *Config

func (*Config) CustomCode

func (c *Config) CustomCode(code string) *Config

CustomCode @Description: Users can use their own generated characters as the verification code, the custom code will be capitalized and then used. Therefore, when validating code, you must use all uppercase code. @receiver c @param code: Customize the generated verification code, but the length limit of 4 digits needs to be met. The custom code will be capitalized and then used. Therefore, when validating code, you must use all uppercase code. @return *Config

func (*Config) GenerateGif added in v1.0.1

func (c *Config) GenerateGif() (gifBase64Data string, code string, err error)

GenerateGif @Description: Generate gif @receiver c @return gifBase64Data @return code @return err

func (*Config) GenerateImage added in v1.0.1

func (c *Config) GenerateImage() (imageBase64Data string, code string, err error)

GenerateImage @Description: Generate a static image @receiver c @return imageBase64Data @return code @return err

type Options

type Options func(*Config)

func SetBackgroundHexColor

func SetBackgroundHexColor(backgroundHexColor string) Options

SetBackgroundHexColor @Description: Set the background color of the captcha image @param backgroundHexColor: Background color, only hex can be used @return Options

func SetCodeType

func SetCodeType(codeType string) Options

SetCodeType @Description: Set the type of random validation characters, which takes effect only if the user is not using custom code @param codeType: Use one of the built-in types: UppercaseEnglishCharacters, NumericCharacters, MixedCharacters @return Options

func SetDevMode

func SetDevMode(devMode bool) Options

SetDevMode @Description: In the development mode, the generated image is saved as a local file for easy viewing @return Options

func SetFontHexColor

func SetFontHexColor(fontHexColor string) Options

SetFontHexColor @Description: Set the font color of the captcha image @param fontHexColor: Font color, only hex can be used @return Options

func SetHeight

func SetHeight(height int) Options

SetHeight @Description: Set the height of the captcha image, which is 120 by default @param height @return Options

func SetLineHexColors

func SetLineHexColors(lineHexColors []string) Options

SetLineHexColors @Description: Set the line color of the captcha image, a minimum of 3 colors need to be set, and the line will randomly get 3 colors from them to draw @param lineHexColors: Font colors, only hex can be used, a minimum of 3 colors need to be set @return Options

func SetWidth

func SetWidth(width int) Options

SetWidth @Description: Set the width of the captcha image, which is 300 by default @param width @return Options

type Theme added in v1.0.1

type Theme struct {
	BackgroundHexColor string
	FontHexColors      []string
	LineHexColors      []string
}

Jump to

Keyboard shortcuts

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