fix: compress image for req

This commit is contained in:
huangbin.beal@163.com
2024-03-25 20:34:03 +08:00
parent d62e645385
commit a69ef2d87f
3 changed files with 23 additions and 4 deletions

View File

@@ -224,8 +224,27 @@ func (dExt *DriverExt) takeScreenShot(fileName string) (raw *bytes.Buffer, path
}
func compressImageBuffer(raw *bytes.Buffer) (compressed *bytes.Buffer, err error) {
// TODO: compress image data
return raw, nil
// 解码原始图像数据
img, format, err := image.Decode(raw)
if err != nil {
return nil, err
}
// 创建一个用来保存压缩后数据的buffer
var buf bytes.Buffer
switch format {
// Convert to jpeg uniformly and compress with a compression rate of 95
case "jpeg", "png":
jpegOptions := &jpeg.Options{Quality: 95}
err = jpeg.Encode(&buf, img, jpegOptions)
default:
return nil, fmt.Errorf("unsupported image format: %s", format)
}
// 返回压缩后的图像数据
return &buf, nil
}
// saveScreenShot saves image file with file name