From c7bebfca34051def220cfdf12f3ada5c80ff9389 Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Fri, 19 Apr 2024 10:06:05 +0800 Subject: [PATCH] Add EXTRA_PARAMS_CROP_INTERESTING (#328) --- config/config.go | 39 ++++++++++++++++++++++++++------------- encoder/process.go | 22 +++++++++++++++++++++- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index 6cac3c8e..78da7fc8 100644 --- a/config/config.go +++ b/config/config.go @@ -33,6 +33,7 @@ const ( "CONVERT_TYPES": ["webp"], "STRIP_METADATA": true, "ENABLE_EXTRA_PARAMS": false, + "EXTRA_PARAMS_CROP_INTERESTING": "InterestingAttention", "READ_BUFFER_SIZE": 4096, "CONCURRENCY": 262144, "DISABLE_KEEPALIVE": false, @@ -49,7 +50,7 @@ var ( ProxyMode bool Prefetch bool Config = NewWebPConfig() - Version = "0.11.1" + Version = "0.11.2" WriteLock = cache.New(5*time.Minute, 10*time.Minute) ConvertLock = cache.New(5*time.Minute, 10*time.Minute) RemoteRaw = "./remote-raw" @@ -78,12 +79,14 @@ type WebpConfig struct { EnableAVIF bool `json:"ENABLE_AVIF"` EnableJXL bool `json:"ENABLE_JXL"` - EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"` - StripMetadata bool `json:"STRIP_METADATA"` - ReadBufferSize int `json:"READ_BUFFER_SIZE"` - Concurrency int `json:"CONCURRENCY"` - DisableKeepalive bool `json:"DISABLE_KEEPALIVE"` - CacheTTL int `json:"CACHE_TTL"` + EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"` + ExtraParamsCropInteresting string `json:"EXTRA_PARAMS_CROP_INTERESTING"` + + StripMetadata bool `json:"STRIP_METADATA"` + ReadBufferSize int `json:"READ_BUFFER_SIZE"` + Concurrency int `json:"CONCURRENCY"` + DisableKeepalive bool `json:"DISABLE_KEEPALIVE"` + CacheTTL int `json:"CACHE_TTL"` } func NewWebPConfig() *WebpConfig { @@ -101,12 +104,13 @@ func NewWebPConfig() *WebpConfig { EnableAVIF: false, EnableJXL: false, - EnableExtraParams: false, - StripMetadata: true, - ReadBufferSize: 4096, - Concurrency: 262144, - DisableKeepalive: false, - CacheTTL: 259200, + EnableExtraParams: false, + ExtraParamsCropInteresting: "InterestingAttention", + StripMetadata: true, + ReadBufferSize: 4096, + Concurrency: 262144, + DisableKeepalive: false, + CacheTTL: 259200, } } @@ -191,6 +195,15 @@ func LoadConfig() { log.Warnf("WEBP_ENABLE_EXTRA_PARAMS is not a valid boolean, using value in config.json %t", Config.EnableExtraParams) } } + if os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING") != "" { + availableInteresting := []string{"InterestingNone", "InterestingEntropy", "InterestingCentre", "InterestingAttention", "InterestringLow", "InterestingHigh", "InterestingAll"} + if slices.Contains(availableInteresting, os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING")) { + Config.ExtraParamsCropInteresting = os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING") + } else { + log.Warnf("WEBP_EXTRA_PARAMS_CROP_INTERESTING is not a valid interesting, using value in config.json %s", Config.ExtraParamsCropInteresting) + } + } + if os.Getenv("WEBP_STRIP_METADATA") != "" { stripMetadata := os.Getenv("WEBP_STRIP_METADATA") if stripMetadata == "true" { diff --git a/encoder/process.go b/encoder/process.go index b149c890..d3c36169 100644 --- a/encoder/process.go +++ b/encoder/process.go @@ -63,7 +63,27 @@ func resizeImage(img *vips.ImageRef, extraParams config.ExtraParams) error { } if extraParams.Width > 0 && extraParams.Height > 0 { - err := img.Thumbnail(extraParams.Width, extraParams.Height, vips.InterestingAttention) + var cropInteresting vips.Interesting + switch config.Config.ExtraParamsCropInteresting { + case "InterestingNone": + cropInteresting = vips.InterestingNone + case "InterestingCentre": + cropInteresting = vips.InterestingCentre + case "InterestingEntropy": + cropInteresting = vips.InterestingEntropy + case "InterestingAttention": + cropInteresting = vips.InterestingAttention + case "InterestingLow": + cropInteresting = vips.InterestingLow + case "InterestingHigh": + cropInteresting = vips.InterestingHigh + case "InterestingAll": + cropInteresting = vips.InterestingAll + default: + cropInteresting = vips.InterestingAttention + } + + err := img.Thumbnail(extraParams.Width, extraParams.Height, cropInteresting) if err != nil { return err }