Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package config

import (
"errors"
"github.com/TestsLing/aj-captcha-go/const"
"github.com/xierui921326/aj-captcha-go/const"
"image/color"
"strings"
)
Expand All @@ -17,6 +17,8 @@ type WatermarkConfig struct {
type BlockPuzzleConfig struct {
// 校验时 容错偏移量
Offset int `yaml:"offset"`
// 是否插入干扰图
IsInterference bool `yaml:"isInterference"`
}

type ClickWordConfig struct {
Expand Down
6 changes: 3 additions & 3 deletions example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"encoding/json"
"errors"
config2 "github.com/TestsLing/aj-captcha-go/config"
"github.com/TestsLing/aj-captcha-go/const"
"github.com/TestsLing/aj-captcha-go/service"
config2 "github.com/xierui921326/aj-captcha-go/config"
"github.com/xierui921326/aj-captcha-go/const"
"github.com/xierui921326/aj-captcha-go/service"
"image/color"
"io/ioutil"
"log"
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/TestsLing/aj-captcha-go
module github.com/xierui921326/aj-captcha-go

go 1.12
go 1.19

require (
github.com/go-redis/redis/v8 v8.11.5
Expand Down
30 changes: 16 additions & 14 deletions service/block_puzzle_captcha_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
constant "github.com/TestsLing/aj-captcha-go/const"
"github.com/TestsLing/aj-captcha-go/model/vo"
"github.com/TestsLing/aj-captcha-go/util"
img "github.com/TestsLing/aj-captcha-go/util/image"
constant "github.com/xierui921326/aj-captcha-go/const"
"github.com/xierui921326/aj-captcha-go/model/vo"
"github.com/xierui921326/aj-captcha-go/util"
img "github.com/xierui921326/aj-captcha-go/util/image"
"golang.org/x/image/colornames"
"log"
"math"
Expand Down Expand Up @@ -40,7 +40,7 @@ func (b *BlockPuzzleCaptchaService) Get() (map[string]interface{}, error) {
templateImage := img.GetTemplateImage()

// 构造前端所需图片
b.pictureTemplatesCut(backgroundImage, templateImage)
b.pictureTemplatesCut(backgroundImage, templateImage, b.factory.config.BlockPuzzle.IsInterference)

originalImageBase64, err := backgroundImage.Base64()
jigsawImageBase64, err := templateImage.Base64()
Expand All @@ -67,20 +67,22 @@ func (b *BlockPuzzleCaptchaService) Get() (map[string]interface{}, error) {
return data, nil
}

func (b *BlockPuzzleCaptchaService) pictureTemplatesCut(backgroundImage *util.ImageUtil, templateImage *util.ImageUtil) {
func (b *BlockPuzzleCaptchaService) pictureTemplatesCut(backgroundImage *util.ImageUtil, templateImage *util.ImageUtil, isInterference bool) {
// 生成拼图坐标点
b.generateJigsawPoint(backgroundImage, templateImage)
// 裁剪模板图
b.cutByTemplate(backgroundImage, templateImage, b.point.X, 0)

// 插入干扰图
for {
newTemplateImage := img.GetTemplateImage()
if newTemplateImage.Src != templateImage.Src {
offsetX := util.RandomInt(0, backgroundImage.Width-newTemplateImage.Width-5)
if math.Abs(float64(newTemplateImage.Width-offsetX)) > float64(newTemplateImage.Width/2) {
b.interferenceByTemplate(backgroundImage, newTemplateImage, offsetX, b.point.Y)
break
if isInterference {
// 插入干扰图
for {
newTemplateImage := img.GetTemplateImage()
if newTemplateImage.Src != templateImage.Src {
offsetX := util.RandomInt(0, backgroundImage.Width-newTemplateImage.Width-5)
if math.Abs(float64(newTemplateImage.Width-offsetX)) > float64(newTemplateImage.Width/2) {
b.interferenceByTemplate(backgroundImage, newTemplateImage, offsetX, b.point.Y)
break
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/captcha_service_factory.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package service

import (
configIns "github.com/TestsLing/aj-captcha-go/config"
configIns "github.com/xierui921326/aj-captcha-go/config"
"log"
"sync"
)
Expand Down
8 changes: 4 additions & 4 deletions service/click_word_captcha_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
constant "github.com/TestsLing/aj-captcha-go/const"
"github.com/TestsLing/aj-captcha-go/model/vo"
"github.com/TestsLing/aj-captcha-go/util"
img "github.com/TestsLing/aj-captcha-go/util/image"
constant "github.com/xierui921326/aj-captcha-go/const"
"github.com/xierui921326/aj-captcha-go/model/vo"
"github.com/xierui921326/aj-captcha-go/util"
img "github.com/xierui921326/aj-captcha-go/util/image"
"log"
)

Expand Down
2 changes: 1 addition & 1 deletion service/mem_cache_service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package service

import (
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/util"
"strconv"
)

Expand Down
2 changes: 1 addition & 1 deletion service/redis_cache_service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package service

import (
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/util"
"strconv"
)

Expand Down
4 changes: 2 additions & 2 deletions test/block_puzzle_captcha_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package test

import (
"fmt"
"github.com/TestsLing/aj-captcha-go/service"
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/service"
"github.com/xierui921326/aj-captcha-go/util"
"image/color"
"testing"
)
Expand Down
2 changes: 1 addition & 1 deletion test/cache_util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/util"
"testing"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion test/image_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
i "github.com/TestsLing/aj-captcha-go/util/image"
i "github.com/xierui921326/aj-captcha-go/util/image"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion test/local_cache_service_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
"github.com/TestsLing/aj-captcha-go/service"
"github.com/xierui921326/aj-captcha-go/service"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion test/random_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package test

import (
"fmt"
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/util"
"testing"
)

Expand Down
2 changes: 1 addition & 1 deletion test/redis_util_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package test

import (
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/util"
"testing"
"time"
)
Expand Down
4 changes: 2 additions & 2 deletions util/image/image.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package image

import (
"github.com/TestsLing/aj-captcha-go/const"
"github.com/TestsLing/aj-captcha-go/util"
"github.com/xierui921326/aj-captcha-go/const"
"github.com/xierui921326/aj-captcha-go/util"
"log"
"os"
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package util
import (
"bytes"
"encoding/base64"
"github.com/TestsLing/aj-captcha-go/model/vo"
"github.com/xierui921326/aj-captcha-go/model/vo"
"github.com/golang/freetype"
"image"
"image/color"
Expand Down