1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-09-16 09:36:18 +02:00

Simplify WebP preset config

This commit is contained in:
DarthSim
2025-06-30 18:28:31 +03:00
parent 4d684fab0a
commit 92a281f834
4 changed files with 71 additions and 27 deletions

View File

@@ -56,7 +56,7 @@ var (
AvifSpeed int
JxlEffort int
WebpEffort int
WebpPreset string
WebpPreset WebpPresetKind
Quality int
FormatQuality map[imagetype.Type]int
StripMetadata bool
@@ -263,7 +263,7 @@ func Reset() {
AvifSpeed = 8
JxlEffort = 4
WebpEffort = 4
WebpPreset = "default"
WebpPreset = WebpPresetDefault
Quality = 80
FormatQuality = map[imagetype.Type]int{
imagetype.WEBP: 79,
@@ -496,7 +496,9 @@ func Configure() error {
configurators.Int(&AvifSpeed, "IMGPROXY_AVIF_SPEED")
configurators.Int(&JxlEffort, "IMGPROXY_JXL_EFFORT")
configurators.Int(&WebpEffort, "IMGPROXY_WEBP_EFFORT")
configurators.String(&WebpPreset, "IMGPROXY_WEBP_PRESET")
if err := configurators.FromMap(&WebpPreset, "IMGPROXY_WEBP_PRESET", WebpPresets); err != nil {
return err
}
configurators.Int(&Quality, "IMGPROXY_QUALITY")
if err := configurators.ImageTypesQuality(FormatQuality, "IMGPROXY_FORMAT_QUALITY"); err != nil {
return err

View File

@@ -289,3 +289,15 @@ func RegexpFromPattern(pattern string) *regexp.Regexp {
// It is safe to use regexp.MustCompile since the expression is always valid
return regexp.MustCompile(result.String())
}
func FromMap[T any](v *T, name string, m map[string]T) error {
if env := os.Getenv(name); len(env) > 0 {
if val, ok := m[env]; ok {
*v = val
} else {
return fmt.Errorf("Invalid %s value: %s", name, env)
}
}
return nil
}

41
config/webp_preset.go Normal file
View File

@@ -0,0 +1,41 @@
package config
import "fmt"
type WebpPresetKind int
const (
WebpPresetDefault WebpPresetKind = iota
WebpPresetPhoto
WebpPresetPicture
WebpPresetDrawing
WebpPresetIcon
WebpPresetText
)
var WebpPresets = map[string]WebpPresetKind{
"default": WebpPresetDefault,
"photo": WebpPresetPhoto,
"picture": WebpPresetPicture,
"drawing": WebpPresetDrawing,
"icon": WebpPresetIcon,
"text": WebpPresetText,
}
func (wp WebpPresetKind) String() string {
for k, v := range WebpPresets {
if v == wp {
return k
}
}
return ""
}
func (wp WebpPresetKind) MarshalJSON() ([]byte, error) {
for k, v := range WebpPresets {
if v == wp {
return []byte(fmt.Sprintf("%q", k)), nil
}
}
return []byte("null"), nil
}

View File

@@ -33,30 +33,10 @@ import (
"github.com/imgproxy/imgproxy/v3/metrics/prometheus"
)
const (
webpPresetDefault = "default"
webpPresetPhoto = "photo"
webpPresetPicture = "picture"
webpPresetDrawing = "drawing"
webpPresetIcon = "icon"
webpPresetText = "text"
)
type Image struct {
VipsImage *C.VipsImage
}
var (
cWebpPreset = map[string]C.VipsForeignWebpPreset{
webpPresetDefault: C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT,
webpPresetPhoto: C.VIPS_FOREIGN_WEBP_PRESET_PHOTO,
webpPresetPicture: C.VIPS_FOREIGN_WEBP_PRESET_PICTURE,
webpPresetDrawing: C.VIPS_FOREIGN_WEBP_PRESET_DRAWING,
webpPresetIcon: C.VIPS_FOREIGN_WEBP_PRESET_ICON,
webpPresetText: C.VIPS_FOREIGN_WEBP_PRESET_TEXT,
}
)
var (
typeSupportLoad sync.Map
typeSupportSave sync.Map
@@ -127,10 +107,19 @@ func Init() error {
vipsConf.PngUnlimited = gbool(config.PngUnlimited)
vipsConf.SvgUnlimited = gbool(config.SvgUnlimited)
if p, ok := cWebpPreset[config.WebpPreset]; ok {
vipsConf.WebpPreset = p
} else {
return newVipsErrorf("invalid libwebp preset: %s", config.WebpPreset)
switch config.WebpPreset {
case config.WebpPresetPhoto:
vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_PHOTO
case config.WebpPresetPicture:
vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_PICTURE
case config.WebpPresetDrawing:
vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_DRAWING
case config.WebpPresetIcon:
vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_ICON
case config.WebpPresetText:
vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_TEXT
default:
vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT
}
prometheus.AddGaugeFunc(