This commit is contained in:
桀尼龟 2025-04-14 12:00:40 +08:00
parent aa61c4d34e
commit 6c3337e733
2 changed files with 47 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package utils
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"image"
"image/color"
@ -18,8 +19,9 @@ import (
)
const (
ImgURL = "https://cdns.fontree.cn/fonchain-main/test/image/artwork/config/slidCode_%d.jpg"
ImgPath = "./code/%d.jpg" //本地路径,暂不使用
ImgURL = "https://cdns.fontree.cn/fonchain-main/test/image/artwork/config/slidCode_%d.jpg"
ImgPath = "./code/%d.jpg" //本地路径,暂不使用
ImgPathPng = "./code/%d.png" //本地路径,暂不使用
)
// 生成指定范围内的随机数
@ -29,41 +31,71 @@ func GetNonceByRange(start, end int) int {
}
// 获取缓冲图片可能是通过URL或本地路径
func GetBufferedImage(place int) (image.Image, error) {
func GetBufferedImage(place int, canvasWidth, canvasHeight int) (draw.Image, error) {
startTime := time.Now()
fmt.Println("")
fmt.Println("")
fmt.Println("")
fmt.Println("1---")
nonce := GetNonceByRange(1, 10)
var imgURL string
//place = 0 // 注意这边的图片现在只使用url图片
if place == 1 {
place = 0 // 注意这边的图片现在只使用url图片
if place == 0 {
fmt.Println("3-1", time.Now().Sub(startTime))
imgURL = fmt.Sprintf(ImgURL, nonce)
resp, err := http.Get(imgURL)
if err != nil {
return nil, err
}
fmt.Println("3-2", time.Now().Sub(startTime))
defer resp.Body.Close()
img, _, err := image.Decode(resp.Body)
img, ff, err := image.Decode(resp.Body)
fmt.Println("----", ff)
if err != nil {
return nil, err
}
return img, nil
} else {
file, err := os.Open(fmt.Sprintf(ImgPath, nonce))
fmt.Println("3-3", time.Now().Sub(startTime))
canvasImage := ImageResize(img, canvasWidth, canvasHeight).(*image.RGBA)
return canvasImage, nil
} else { //从redis中获取
file, err := os.Open(fmt.Sprintf(ImgPathPng, nonce))
fmt.Println("4-1", time.Now().Sub(startTime))
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
//img, format, err := image.Decode(file)
img, err := png.Decode(file)
format := "1"
fmt.Println("", err)
//img, err := jpeg.Decode(file)
fmt.Println("4-1-1", time.Now().Sub(startTime), err)
fmt.Println("4-2", time.Now().Sub(startTime), format)
if err != nil {
return nil, err
}
return img, nil
ss := ImageResize(img, canvasWidth, canvasHeight)
canvasImage := ss.(*image.RGBA)
return canvasImage, nil
}
return nil, errors.New("类型错误")
}
// 调整图片大小
func ImageResize(bufferedImage image.Image, width, height int) image.Image {
return resize.Resize(uint(width), uint(height), bufferedImage, resize.Lanczos3)
return resize.Resize(uint(width), uint(height), bufferedImage, resize.Bilinear)
}
// CutByTemplate 根据模板裁剪图像 // 生成圆角正方形的滑块图像

View File

@ -757,8 +757,8 @@ func (a *AccountProvider) GenerateSliderCaptcha(_ context.Context, in *account.G
//blockRadius := captcha.BlockRadius
place := captcha.Place
img, _ := utils.GetBufferedImage(place)
canvasImage := utils.ImageResize(img, canvasWidth, canvasHeight).(*image.RGBA)
canvasImage, _ := utils.GetBufferedImage(place, canvasWidth, canvasHeight)
//canvasImage := utils.ImageResize(img, canvasWidth, canvasHeight).(*image.RGBA)
blockX := utils.GetNonceByRange(blockWidth, canvasWidth-blockWidth-10)
blockY := utils.GetNonceByRange(10, canvasHeight-blockHeight+1)
blockImage := image.NewRGBA(image.Rect(0, 0, blockWidth, blockHeight))