Skip to content

Commit

Permalink
Merge pull request #11 from reu98/feat/add-generate-with-text
Browse files Browse the repository at this point in the history
feat: add option generate with text
  • Loading branch information
reu98 authored Mar 27, 2024
2 parents 28afba5 + 2370234 commit df40205
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion captcha.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ type Result struct {
// CreateByText: generate a new captcha
func CreateByText(option OptionText) (*Result, error) {
opt := getOptionByText(option)
text := opt.randomText()
text := opt.text
if text == "" {
text = opt.randomText()
}

data, err := opt.createCaptcha(text)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions captcha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ func TestCreateCaptcha(t *testing.T) {
require.NotEmpty(t, capt)
require.NoError(t, err)
}

func TestCreateCaptchaWithText(t *testing.T) {
opt := getOptionByText(OptionText{
Text: "abcd",
})
capt, err := opt.createCaptcha(opt.text)

require.NotEmpty(t, capt)
require.NoError(t, err)
}
6 changes: 6 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type OptionText struct {
// The characters that can be displayed in the captcha.
// Default: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
CharactersPreset string

// Generate With Text
// Note: Text is optional. If not provided, it will be generated randomly
Text string
}

type OptionMath struct {
Expand Down Expand Up @@ -100,6 +104,7 @@ type OptionMath struct {

type option struct {
size uint8
text string
width uint16
height uint16
isColor bool
Expand Down Expand Up @@ -165,6 +170,7 @@ func getOptionByText(opt OptionText) *option {
ignoreCharacters: opt.IgnoreCharacters,
charactersPreset: charPreset,
fontPath: opt.FontPath,
text: opt.Text,
}
}

Expand Down

0 comments on commit df40205

Please sign in to comment.