Skip to content

Commit c7bebfc

Browse files
authored
Add EXTRA_PARAMS_CROP_INTERESTING (#328)
1 parent b5842b2 commit c7bebfc

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

config/config.go

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
"CONVERT_TYPES": ["webp"],
3434
"STRIP_METADATA": true,
3535
"ENABLE_EXTRA_PARAMS": false,
36+
"EXTRA_PARAMS_CROP_INTERESTING": "InterestingAttention",
3637
"READ_BUFFER_SIZE": 4096,
3738
"CONCURRENCY": 262144,
3839
"DISABLE_KEEPALIVE": false,
@@ -49,7 +50,7 @@ var (
4950
ProxyMode bool
5051
Prefetch bool
5152
Config = NewWebPConfig()
52-
Version = "0.11.1"
53+
Version = "0.11.2"
5354
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
5455
ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
5556
RemoteRaw = "./remote-raw"
@@ -78,12 +79,14 @@ type WebpConfig struct {
7879
EnableAVIF bool `json:"ENABLE_AVIF"`
7980
EnableJXL bool `json:"ENABLE_JXL"`
8081

81-
EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"`
82-
StripMetadata bool `json:"STRIP_METADATA"`
83-
ReadBufferSize int `json:"READ_BUFFER_SIZE"`
84-
Concurrency int `json:"CONCURRENCY"`
85-
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"`
86-
CacheTTL int `json:"CACHE_TTL"`
82+
EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"`
83+
ExtraParamsCropInteresting string `json:"EXTRA_PARAMS_CROP_INTERESTING"`
84+
85+
StripMetadata bool `json:"STRIP_METADATA"`
86+
ReadBufferSize int `json:"READ_BUFFER_SIZE"`
87+
Concurrency int `json:"CONCURRENCY"`
88+
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"`
89+
CacheTTL int `json:"CACHE_TTL"`
8790
}
8891

8992
func NewWebPConfig() *WebpConfig {
@@ -101,12 +104,13 @@ func NewWebPConfig() *WebpConfig {
101104
EnableAVIF: false,
102105
EnableJXL: false,
103106

104-
EnableExtraParams: false,
105-
StripMetadata: true,
106-
ReadBufferSize: 4096,
107-
Concurrency: 262144,
108-
DisableKeepalive: false,
109-
CacheTTL: 259200,
107+
EnableExtraParams: false,
108+
ExtraParamsCropInteresting: "InterestingAttention",
109+
StripMetadata: true,
110+
ReadBufferSize: 4096,
111+
Concurrency: 262144,
112+
DisableKeepalive: false,
113+
CacheTTL: 259200,
110114
}
111115
}
112116

@@ -191,6 +195,15 @@ func LoadConfig() {
191195
log.Warnf("WEBP_ENABLE_EXTRA_PARAMS is not a valid boolean, using value in config.json %t", Config.EnableExtraParams)
192196
}
193197
}
198+
if os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING") != "" {
199+
availableInteresting := []string{"InterestingNone", "InterestingEntropy", "InterestingCentre", "InterestingAttention", "InterestringLow", "InterestingHigh", "InterestingAll"}
200+
if slices.Contains(availableInteresting, os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING")) {
201+
Config.ExtraParamsCropInteresting = os.Getenv("WEBP_EXTRA_PARAMS_CROP_INTERESTING")
202+
} else {
203+
log.Warnf("WEBP_EXTRA_PARAMS_CROP_INTERESTING is not a valid interesting, using value in config.json %s", Config.ExtraParamsCropInteresting)
204+
}
205+
}
206+
194207
if os.Getenv("WEBP_STRIP_METADATA") != "" {
195208
stripMetadata := os.Getenv("WEBP_STRIP_METADATA")
196209
if stripMetadata == "true" {

encoder/process.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,27 @@ func resizeImage(img *vips.ImageRef, extraParams config.ExtraParams) error {
6363
}
6464

6565
if extraParams.Width > 0 && extraParams.Height > 0 {
66-
err := img.Thumbnail(extraParams.Width, extraParams.Height, vips.InterestingAttention)
66+
var cropInteresting vips.Interesting
67+
switch config.Config.ExtraParamsCropInteresting {
68+
case "InterestingNone":
69+
cropInteresting = vips.InterestingNone
70+
case "InterestingCentre":
71+
cropInteresting = vips.InterestingCentre
72+
case "InterestingEntropy":
73+
cropInteresting = vips.InterestingEntropy
74+
case "InterestingAttention":
75+
cropInteresting = vips.InterestingAttention
76+
case "InterestingLow":
77+
cropInteresting = vips.InterestingLow
78+
case "InterestingHigh":
79+
cropInteresting = vips.InterestingHigh
80+
case "InterestingAll":
81+
cropInteresting = vips.InterestingAll
82+
default:
83+
cropInteresting = vips.InterestingAttention
84+
}
85+
86+
err := img.Thumbnail(extraParams.Width, extraParams.Height, cropInteresting)
6787
if err != nil {
6888
return err
6989
}

0 commit comments

Comments
 (0)