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"
@ -79,6 +80,8 @@ type WebpConfig struct {
EnableJXL bool `json:"ENABLE_JXL"` EnableJXL bool `json:"ENABLE_JXL"`
EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"` EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"`
ExtraParamsCropInteresting string `json:"EXTRA_PARAMS_CROP_INTERESTING"`
StripMetadata bool `json:"STRIP_METADATA"` StripMetadata bool `json:"STRIP_METADATA"`
ReadBufferSize int `json:"READ_BUFFER_SIZE"` ReadBufferSize int `json:"READ_BUFFER_SIZE"`
Concurrency int `json:"CONCURRENCY"` Concurrency int `json:"CONCURRENCY"`
@ -102,6 +105,7 @@ func NewWebPConfig() *WebpConfig {
EnableJXL: false, EnableJXL: false,
EnableExtraParams: false, EnableExtraParams: false,
ExtraParamsCropInteresting: "InterestingAttention",
StripMetadata: true, StripMetadata: true,
ReadBufferSize: 4096, ReadBufferSize: 4096,
Concurrency: 262144, Concurrency: 262144,
@ -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
} }