WebP转换质量转移到WebP策略配置

This commit is contained in:
GoEdgeLab
2023-12-11 10:17:17 +08:00
parent 9641eb200f
commit 142a726102

View File

@@ -67,6 +67,7 @@ type HTTPWriter struct {
// WebP // WebP
webpIsEncoding bool webpIsEncoding bool
webpOriginContentType string webpOriginContentType string
webpQuality int
// Compression // Compression
compressionConfig *serverconfigs.HTTPCompressionConfig compressionConfig *serverconfigs.HTTPCompressionConfig
@@ -470,8 +471,8 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) {
contentTypeWritten = true contentTypeWritten = true
} }
err := cacheWriter.WriteAt(start, data) writeErr := cacheWriter.WriteAt(start, data)
if err != nil { if writeErr != nil {
hasError = true hasError = true
this.cacheIsFinished = false this.cacheIsFinished = false
} }
@@ -518,6 +519,7 @@ func (this *HTTPWriter) PrepareWebP(resp *http.Response, size int64) {
if policy.RequireCache && this.req.cacheRef == nil { if policy.RequireCache && this.req.cacheRef == nil {
return return
} }
this.webpQuality = policy.Quality
// 限制最小和最大尺寸 // 限制最小和最大尺寸
// TODO 需要将reader修改为LimitReader // TODO 需要将reader修改为LimitReader
@@ -1092,9 +1094,17 @@ func (this *HTTPWriter) finishWebP() {
return return
} }
var f = types.Float32(this.req.web.WebP.Quality) var f = types.Float32(this.webpQuality)
if f > 100 { if f <= 0 || f > 100 {
f = 100 if this.size > (8<<20) || this.size <= 0 {
f = 30
} else if this.size > (1 << 20) {
f = 50
} else if this.size > (128 << 10) {
f = 60
} else {
f = 75
}
} }
if imageData != nil { if imageData != nil {