Skip to content

Commit 5af35e0

Browse files
committed
refactor: 用imaging库替代现有功能
1 parent d1005d0 commit 5af35e0

File tree

4 files changed

+9
-109
lines changed

4 files changed

+9
-109
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ require (
1717
github.com/gin-contrib/cors v1.7.6
1818
github.com/gin-gonic/gin v1.10.1
1919
github.com/pkg/xattr v0.4.12
20-
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
2120
github.com/satori/go.uuid v1.2.0
2221
github.com/spf13/viper v1.21.0
2322
github.com/zjutjh/WeJH-SDK v0.2.4

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
103103
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
104104
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
105105
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
106-
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
107-
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
108106
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
109107
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
110108
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=

internal/services/objectService/decode.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

internal/services/objectService/service.go

Lines changed: 9 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ import (
1818
"github.com/chai2010/webp"
1919
"github.com/disintegration/imaging"
2020
"github.com/dustin/go-humanize"
21-
"github.com/rwcarlsen/goexif/exif"
22-
"golang.org/x/image/draw"
21+
_ "golang.org/x/image/webp" // 注册解码器
2322
)
2423

2524
// SizeLimit 上传大小限制
2625
var SizeLimit = humanize.MiByte * config.Config.GetInt64("oss.limit")
2726

27+
var maxLongEdge = config.Config.GetInt("oss.thumbnailLongEdge")
28+
2829
var invalidCharRegex = regexp.MustCompile(`[:*?"<>|]`)
2930

3031
var genLocks sync.Map
@@ -53,16 +54,10 @@ func CleanLocation(location string) string {
5354

5455
// ConvertToWebP 将图片转换为 WebP 格式
5556
func ConvertToWebP(reader io.Reader) (*bytes.Reader, error) {
56-
data, err := io.ReadAll(reader)
57-
if err != nil {
58-
return nil, err
59-
}
60-
61-
img, _, err := image.Decode(bytes.NewReader(data))
57+
img, err := imaging.Decode(reader, imaging.AutoOrientation(true))
6258
if err != nil {
6359
return nil, err
6460
}
65-
img, _ = fixOrientation(img, bytes.NewReader(data))
6661

6762
buf, _ := bufferPool.Get().(*bytes.Buffer)
6863
buf.Reset()
@@ -78,50 +73,6 @@ func ConvertToWebP(reader io.Reader) (*bytes.Reader, error) {
7873
return bytes.NewReader(buf.Bytes()), nil
7974
}
8075

81-
// fixOrientation 修复图片的旋转
82-
func fixOrientation(img image.Image, exifData io.Reader) (image.Image, error) {
83-
x, err := exif.Decode(exifData)
84-
if err != nil {
85-
return nil, err
86-
}
87-
88-
tag, err := x.Get(exif.Orientation)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
orientation, err := tag.Int(0)
94-
if err != nil {
95-
return nil, err
96-
}
97-
98-
return applyOrientation(img, orientation), nil
99-
}
100-
101-
// applyOrientation 根据 EXIF Orientation 调整图像方向
102-
func applyOrientation(img image.Image, orientation int) image.Image {
103-
switch orientation {
104-
case 1: // 正常
105-
return img
106-
case 2: // 水平翻转
107-
return imaging.FlipH(img)
108-
case 3: // 旋转 180°
109-
return imaging.Rotate180(img)
110-
case 4: // 垂直翻转
111-
return imaging.FlipV(img)
112-
case 5: // 顺时针 90° + 水平翻转
113-
return imaging.FlipH(imaging.Rotate270(img))
114-
case 6: // 顺时针 90°
115-
return imaging.Rotate270(img)
116-
case 7: // 顺时针 90° + 垂直翻转
117-
return imaging.FlipV(imaging.Rotate270(img))
118-
case 8: // 逆时针 90°
119-
return imaging.Rotate90(img)
120-
default:
121-
return img
122-
}
123-
}
124-
12576
// GetThumbnail 获取缩略图
12677
func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error) {
12778
filename := bucket + "-" + objectKey
@@ -163,12 +114,12 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
163114
}()
164115

165116
// 解码图片
166-
img, err := decodeImg(object)
117+
img, err := imaging.Decode(object, imaging.AutoOrientation(true))
167118
if err != nil {
168119
return nil, 0, err
169120
}
170121
img = removeAlpha(img)
171-
finalImg := resizeIfNeeded(img, config.Config.GetInt("oss.thumbnailLongEdge"))
122+
finalImg := imaging.Fit(img, maxLongEdge, maxLongEdge, imaging.CatmullRom)
172123

173124
buf, _ := bufferPool.Get().(*bytes.Buffer)
174125
buf.Reset()
@@ -189,29 +140,6 @@ func GetThumbnail(bucket string, objectKey string) (io.ReadCloser, int64, error)
189140
return io.NopCloser(bytes.NewReader(buf.Bytes())), int64(buf.Len()), nil
190141
}
191142

192-
func resizeIfNeeded(img image.Image, targetLongSide int) image.Image {
193-
b := img.Bounds()
194-
w, h := b.Dx(), b.Dy()
195-
longSide, shortSide := max(w, h), min(w, h)
196-
197-
if longSide <= targetLongSide {
198-
return img
199-
}
200-
scale := float64(targetLongSide) / float64(longSide)
201-
targetShort := int(float64(shortSide) * scale)
202-
203-
var tw, th int
204-
if w > h {
205-
tw, th = targetLongSide, targetShort
206-
} else {
207-
tw, th = targetShort, targetLongSide
208-
}
209-
210-
dst := image.NewRGBA(image.Rect(0, 0, tw, th))
211-
draw.BiLinear.Scale(dst, dst.Rect, img, b, draw.Over, nil)
212-
return dst
213-
}
214-
215143
func waitForPath(p string) (first bool, done func()) {
216144
ch := make(chan struct{})
217145
actual, loaded := genLocks.LoadOrStore(p, ch)
@@ -229,11 +157,8 @@ func waitForPath(p string) (first bool, done func()) {
229157
return false, func() {}
230158
}
231159

232-
func removeAlpha(img image.Image) *image.RGBA {
160+
func removeAlpha(img image.Image) image.Image {
233161
b := img.Bounds()
234-
out := image.NewRGBA(b)
235-
236-
draw.Draw(out, b, &image.Uniform{C: color.White}, image.Point{}, draw.Src)
237-
draw.Draw(out, b, img, b.Min, draw.Over)
238-
return out
162+
bg := imaging.New(b.Dx(), b.Dy(), color.White)
163+
return imaging.Overlay(bg, img, image.Pt(0, 0), 1.0)
239164
}

0 commit comments

Comments
 (0)