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

View File

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

View File

@ -757,8 +757,8 @@ func (a *AccountProvider) GenerateSliderCaptcha(_ context.Context, in *account.G
//blockRadius := captcha.BlockRadius
place := captcha.Place
canvasImage, _ := utils.GetBufferedImage(place, canvasWidth, canvasHeight)
//canvasImage := utils.ImageResize(img, canvasWidth, canvasHeight).(*image.RGBA)
img, _ := utils.GetBufferedImage(place)
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))