Add EXTRA_PARAMS_CROP_INTERESTING (#328)

This commit is contained in:
Nova Kwok 2024-04-19 10:06:05 +08:00 committed by GitHub
parent b5842b264c
commit c7bebfca34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 14 deletions

View File

@ -33,6 +33,7 @@ const (
"CONVERT_TYPES": ["webp"], "CONVERT_TYPES": ["webp"],
"STRIP_METADATA": true, "STRIP_METADATA": true,
"ENABLE_EXTRA_PARAMS": false, "ENABLE_EXTRA_PARAMS": false,
"EXTRA_PARAMS_CROP_INTERESTING": "InterestingAttention",
"READ_BUFFER_SIZE": 4096, "READ_BUFFER_SIZE": 4096,
"CONCURRENCY": 262144, "CONCURRENCY": 262144,
"DISABLE_KEEPALIVE": false, "DISABLE_KEEPALIVE": false,
@ -49,7 +50,7 @@ var (
ProxyMode bool ProxyMode bool
Prefetch bool Prefetch bool
Config = NewWebPConfig() Config = NewWebPConfig()
Version = "0.11.1" Version = "0.11.2"
WriteLock = cache.New(5*time.Minute, 10*time.Minute) WriteLock = cache.New(5*time.Minute, 10*time.Minute)
ConvertLock = cache.New(5*time.Minute, 10*time.Minute) ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
RemoteRaw = "./remote-raw" RemoteRaw = "./remote-raw"
@ -78,12 +79,14 @@ type WebpConfig struct {
EnableAVIF bool `json:"ENABLE_AVIF"` EnableAVIF bool `json:"ENABLE_AVIF"`
EnableJXL bool `json:"ENABLE_JXL"` EnableJXL bool `json:"ENABLE_JXL"`
EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"` EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"`
StripMetadata bool `json:"STRIP_METADATA"` ExtraParamsCropInteresting string `json:"EXTRA_PARAMS_CROP_INTERESTING"`
ReadBufferSize int `json:"READ_BUFFER_SIZE"`
Concurrency int `json:"CONCURRENCY"` StripMetadata bool `json:"STRIP_METADATA"`
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"` ReadBufferSize int `json:"READ_BUFFER_SIZE"`
CacheTTL int `json:"CACHE_TTL"` Concurrency int `json:"CONCURRENCY"`
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"`
CacheTTL int `json:"CACHE_TTL"`
} }
func NewWebPConfig() *WebpConfig { func NewWebPConfig() *WebpConfig {
@ -101,12 +104,13 @@ func NewWebPConfig() *WebpConfig {
EnableAVIF: false, EnableAVIF: false,
EnableJXL: false, EnableJXL: false,
EnableExtraParams: false, EnableExtraParams: false,
StripMetadata: true, ExtraParamsCropInteresting: "InterestingAttention",
ReadBufferSize: 4096, StripMetadata: true,
Concurrency: 262144, ReadBufferSize: 4096,
DisableKeepalive: false, Concurrency: 262144,
CacheTTL: 259200, 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) 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") != "" { if os.Getenv("WEBP_STRIP_METADATA") != "" {
stripMetadata := os.Getenv("WEBP_STRIP_METADATA") stripMetadata := os.Getenv("WEBP_STRIP_METADATA")
if stripMetadata == "true" { if stripMetadata == "true" {

View File

@ -63,7 +63,27 @@ func resizeImage(img *vips.ImageRef, extraParams config.ExtraParams) error {
} }
if extraParams.Width > 0 && extraParams.Height > 0 { 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 { if err != nil {
return err return err
} }