Skip to content

Commit e50bddc

Browse files
committed
chore: Update version to 0.7.8
1 parent 639b3c5 commit e50bddc

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version string = "0.7.7"
10+
Version string = "0.7.8"
1111
)
1212

1313
var VersionCmd = &cobra.Command{

common/image.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ func ResizeImage(img image.Image, width, height uint) image.Image {
9191
return resizedImg
9292
}
9393

94-
// 压缩图像为指定的大小, 返回格式为JPEG
9594
func CompressImage(input []byte, maxSizeMB, maxEdgeLength uint) ([]byte, error) {
9695
img, _, err := image.Decode(bytes.NewReader(input))
9796
if err != nil {
@@ -100,8 +99,8 @@ func CompressImage(input []byte, maxSizeMB, maxEdgeLength uint) ([]byte, error)
10099
bounds := img.Bounds()
101100
width := bounds.Dx()
102101
height := bounds.Dy()
102+
var newWidth, newHeight uint
103103
if width > int(maxEdgeLength) || height > int(maxEdgeLength) {
104-
var newWidth, newHeight uint
105104
if width > height {
106105
newWidth = maxEdgeLength
107106
newHeight = uint(float64(height) * (float64(maxEdgeLength) / float64(width)))
@@ -113,26 +112,26 @@ func CompressImage(input []byte, maxSizeMB, maxEdgeLength uint) ([]byte, error)
113112
}
114113
quality := 100
115114
var buf bytes.Buffer
116-
if len(input) < int(maxSizeMB*1024*1024) {
117-
err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
118-
if err != nil {
119-
return nil, err
120-
}
115+
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
116+
if err != nil {
117+
return nil, err
118+
}
119+
if buf.Len() < int(maxSizeMB*1024*1024) {
121120
return buf.Bytes(), nil
122121
}
123122
for {
123+
if quality <= 0 {
124+
return nil, fmt.Errorf("cannot compress image to %d MB", maxSizeMB)
125+
}
126+
quality -= 5
124127
buf.Reset()
125-
err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
128+
err = jpeg.Encode(&buf, img, &jpeg.Options{Quality: quality})
126129
if err != nil {
127130
return nil, err
128131
}
129132
if buf.Len() < int(maxSizeMB*1024*1024) {
130133
break
131134
}
132-
quality -= 5
133-
if quality <= 0 {
134-
return nil, fmt.Errorf("can not to compress image to %d MB", maxSizeMB)
135-
}
136135
}
137136
return buf.Bytes(), nil
138137
}

0 commit comments

Comments
 (0)