From 13ce72ed928bcd3eb69ac986e5e3cb84fe800929 Mon Sep 17 00:00:00 2001 From: "Autumn.home" Date: Fri, 13 Dec 2024 23:28:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9B=BE=E7=89=87=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/utils/utils.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index b2123e27..ed4a10e8 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -10,6 +10,7 @@ package utils import ( "archive/zip" "bufio" + "bytes" "context" "crypto/md5" "encoding/base64" @@ -22,11 +23,15 @@ import ( "github.com/Autumn-27/ScopeSentry-Scan/internal/types" "github.com/Autumn-27/ScopeSentry-Scan/pkg/logger" "github.com/hbollon/go-edlib" + "github.com/nfnt/resize" "github.com/projectdiscovery/cdncheck" "github.com/projectdiscovery/httpx/runner" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/mem" "gopkg.in/yaml.v3" + "image" + "image/jpeg" + "image/png" "io" "io/ioutil" "math" @@ -1079,3 +1084,54 @@ func (t *UtilTools) HandleLinuxTemp() { return } } + +// CompressAndEncodeScreenshot 压缩图片并返回 Base64 编码的字符串。 +// 它接收原始图片字节流 r.ScreenshotBytes,返回 Base64 编码的图片字符串。 +// 该函数会无损压缩 PNG 图像并缩小为原尺寸的 scaleFactor。 0.5 = 50% +func (t *UtilTools) CompressAndEncodeScreenshot(screenshotBytes []byte, scaleFactor float64) (string, string) { + if screenshotBytes == nil { + logger.SlogWarn("No screenshot data provided.") + return "", "" + } + + // 解码原始图片数据 + img, imgType, err := image.Decode(bytes.NewReader(screenshotBytes)) + if err != nil { + logger.SlogWarn(fmt.Sprintf("Error decoding image:", err)) + return "", "" + } + + // 计算新的图片尺寸(通过缩放比例) + newWidth := uint(float64(img.Bounds().Dx()) * scaleFactor) + newHeight := uint(float64(img.Bounds().Dy()) * scaleFactor) + + // 压缩图片(无损或有损方式,调整尺寸) + compressedImg := resize.Resize(newWidth, newHeight, img, resize.Lanczos3) + + // 创建字节缓冲区存放压缩后的图片数据 + var buf bytes.Buffer + + // 根据图片格式进行不同的编码 + switch imgType { + case "jpeg": + // JPEG 格式使用有损压缩 + err = jpeg.Encode(&buf, compressedImg, nil) + if err != nil { + + logger.SlogWarn(fmt.Sprintf("Error encoding JPEG image:", err)) + return "", "" + } + case "png": + // PNG 格式使用无损压缩 + err = png.Encode(&buf, compressedImg) + if err != nil { + logger.SlogWarn(fmt.Sprintf("Error encoding PNG image:", err)) + return "", "" + } + default: + logger.SlogWarn(fmt.Sprintf("Unsupported image format:", imgType)) + return "", "" + } + + return imgType, base64.StdEncoding.EncodeToString(buf.Bytes()) +}