feat: 重构消息处理模块,引入统一的消息接口和类型安全的消息解析
This commit is contained in:
166
util/picture.go
Normal file
166
util/picture.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/nfnt/resize"
|
||||
)
|
||||
|
||||
func ResizeImage(imagePath string, width int, height int) (outputPath string, err error) {
|
||||
// 打开源图片文件
|
||||
file, err := os.Open(imagePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 解码图片
|
||||
var img image.Image
|
||||
var decodeErr error
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(imagePath))
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg":
|
||||
img, decodeErr = jpeg.Decode(file)
|
||||
case ".png":
|
||||
img, decodeErr = png.Decode(file)
|
||||
default:
|
||||
return "", errors.New("unsupported image format")
|
||||
}
|
||||
|
||||
if decodeErr != nil {
|
||||
return "", decodeErr
|
||||
}
|
||||
|
||||
// 调整图片大小
|
||||
resized := resize.Resize(uint(width), uint(height), img, resize.Lanczos3)
|
||||
|
||||
// 创建输出文件
|
||||
outputPath = strings.TrimSuffix(imagePath, ext) + "_resized" + ext
|
||||
out, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// 根据文件扩展名选择编码方式
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg":
|
||||
return outputPath, jpeg.Encode(out, resized, nil)
|
||||
case ".png":
|
||||
return outputPath, png.Encode(out, resized)
|
||||
default:
|
||||
return "", errors.New("unsupported image format")
|
||||
}
|
||||
}
|
||||
|
||||
// ResizeImageByMaxWidth 按最大宽度缩放图片,保持宽高比
|
||||
func ResizeImageByMaxWidth(imagePath string, maxWidth uint) (outputPath string, err error) {
|
||||
// 打开源图片文件
|
||||
file, err := os.Open(imagePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 解码图片
|
||||
var img image.Image
|
||||
var decodeErr error
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(imagePath))
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg":
|
||||
img, decodeErr = jpeg.Decode(file)
|
||||
case ".png":
|
||||
img, decodeErr = png.Decode(file)
|
||||
default:
|
||||
return "", errors.New("unsupported image format")
|
||||
}
|
||||
|
||||
if decodeErr != nil {
|
||||
return "", decodeErr
|
||||
}
|
||||
|
||||
// 计算缩放后的尺寸,保持宽高比
|
||||
// 传入0作为高度,resize包会自动计算等比例的高度
|
||||
resized := resize.Resize(maxWidth, 0, img, resize.Lanczos3)
|
||||
|
||||
// 创建输出文件
|
||||
outputPath = strings.TrimSuffix(imagePath, ext) + "_resized" + ext
|
||||
out, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// 根据文件扩展名选择编码方式
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg":
|
||||
return outputPath, jpeg.Encode(out, resized, nil)
|
||||
case ".png":
|
||||
return outputPath, png.Encode(out, resized)
|
||||
default:
|
||||
return "", errors.New("unsupported image format")
|
||||
}
|
||||
}
|
||||
|
||||
// ResizeImageByMaxHeight 按最大高度缩放图片,保持宽高比
|
||||
func ResizeImageByMaxHeight(imagePath string, maxHeight uint) (outputPath string, err error) {
|
||||
// 打开源图片文件
|
||||
file, err := os.Open(imagePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 解码图片
|
||||
var img image.Image
|
||||
var decodeErr error
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(imagePath))
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg":
|
||||
img, decodeErr = jpeg.Decode(file)
|
||||
case ".png":
|
||||
img, decodeErr = png.Decode(file)
|
||||
default:
|
||||
return "", errors.New("unsupported image format")
|
||||
}
|
||||
|
||||
if decodeErr != nil {
|
||||
return "", decodeErr
|
||||
}
|
||||
|
||||
// 计算缩放后的尺寸,保持宽高比
|
||||
// 传入0作为宽度,resize包会自动计算等比例的宽度
|
||||
resized := resize.Resize(0, maxHeight, img, resize.Lanczos3)
|
||||
|
||||
// 创建输出文件
|
||||
outputPath = strings.TrimSuffix(imagePath, ext) + "_resized" + ext
|
||||
out, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// 根据文件扩展名选择编码方式
|
||||
switch ext {
|
||||
case ".jpg", ".jpeg":
|
||||
return outputPath, jpeg.Encode(out, resized, nil)
|
||||
case ".png":
|
||||
return outputPath, png.Encode(out, resized)
|
||||
default:
|
||||
return "", errors.New("unsupported image format")
|
||||
}
|
||||
}
|
||||
|
||||
func GetResizedIamgePathByOrgPath(orgPath string) string {
|
||||
ext := strings.ToLower(filepath.Ext(orgPath))
|
||||
return strings.TrimSuffix(orgPath, ext) + "_resized" + ext
|
||||
}
|
||||
@@ -36,11 +36,10 @@ func normalizeURL(rawURL string) string {
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func DownloadFile(url string, filepath string) error {
|
||||
func DownloadFile(url string, filepath string) (err error) {
|
||||
// 发送 HTTP GET 请求
|
||||
// resp, err := http.Get(url)
|
||||
var resp *http.Response
|
||||
var err error
|
||||
var maxRetry = 100
|
||||
var retry = 0
|
||||
for resp, err = http.Get(url); err != nil && retry < maxRetry; resp, err = http.Get(url) {
|
||||
|
||||
Reference in New Issue
Block a user