1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-11-27 22:48:53 +02:00

feat(): Add libvips's equivalent options for configuring libwebp's effort and preset.

This commit is contained in:
hossein
2025-05-24 14:58:56 +03:30
parent dccfe80349
commit 4d684fab0a
4 changed files with 46 additions and 3 deletions

View File

@@ -55,6 +55,8 @@ var (
PngQuantizationColors int
AvifSpeed int
JxlEffort int
WebpEffort int
WebpPreset string
Quality int
FormatQuality map[imagetype.Type]int
StripMetadata bool
@@ -260,6 +262,8 @@ func Reset() {
PngQuantizationColors = 256
AvifSpeed = 8
JxlEffort = 4
WebpEffort = 4
WebpPreset = "default"
Quality = 80
FormatQuality = map[imagetype.Type]int{
imagetype.WEBP: 79,
@@ -491,6 +495,8 @@ func Configure() error {
configurators.Int(&PngQuantizationColors, "IMGPROXY_PNG_QUANTIZATION_COLORS")
configurators.Int(&AvifSpeed, "IMGPROXY_AVIF_SPEED")
configurators.Int(&JxlEffort, "IMGPROXY_JXL_EFFORT")
configurators.Int(&WebpEffort, "IMGPROXY_WEBP_EFFORT")
configurators.String(&WebpPreset, "IMGPROXY_WEBP_PRESET")
configurators.Int(&Quality, "IMGPROXY_QUALITY")
if err := configurators.ImageTypesQuality(FormatQuality, "IMGPROXY_FORMAT_QUALITY"); err != nil {
return err
@@ -750,6 +756,12 @@ func Configure() error {
return fmt.Errorf("JXL effort can't be greater than 9, now - %d\n", JxlEffort)
}
if WebpEffort < 1 {
return fmt.Errorf("Webp effort should be greater than 0, now - %d\n", WebpEffort)
} else if WebpEffort > 6 {
return fmt.Errorf("Webp effort can't be greater than 9, now - %d\n", WebpEffort)
}
if Quality <= 0 {
return fmt.Errorf("Quality should be greater than 0, now - %d\n", Quality)
} else if Quality > 100 {

View File

@@ -1066,11 +1066,13 @@ vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quant
}
int
vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality)
vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort, VipsForeignWebpPreset preset)
{
return vips_webpsave_buffer(
in, buf, len,
"Q", quality,
"effort", effort,
"preset", preset,
NULL);
}

View File

@@ -33,10 +33,30 @@ 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
@@ -51,6 +71,8 @@ var vipsConf struct {
PngQuantizationColors C.int
AvifSpeed C.int
JxlEffort C.int
WebpEffort C.int
WebpPreset C.VipsForeignWebpPreset
PngUnlimited C.int
SvgUnlimited C.int
}
@@ -101,9 +123,16 @@ func Init() error {
vipsConf.PngQuantizationColors = C.int(config.PngQuantizationColors)
vipsConf.AvifSpeed = C.int(config.AvifSpeed)
vipsConf.JxlEffort = C.int(config.JxlEffort)
vipsConf.WebpEffort = C.int(config.WebpEffort)
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)
}
prometheus.AddGaugeFunc(
"vips_memory_bytes",
"A gauge of the vips tracked memory usage in bytes.",
@@ -425,7 +454,7 @@ func (img *Image) Save(imgtype imagetype.Type, quality int) (*imagedata.ImageDat
case imagetype.PNG:
err = C.vips_pngsave_go(img.VipsImage, &ptr, &imgsize, vipsConf.PngInterlaced, vipsConf.PngQuantize, vipsConf.PngQuantizationColors)
case imagetype.WEBP:
err = C.vips_webpsave_go(img.VipsImage, &ptr, &imgsize, C.int(quality))
err = C.vips_webpsave_go(img.VipsImage, &ptr, &imgsize, C.int(quality), vipsConf.WebpEffort, vipsConf.WebpPreset)
case imagetype.GIF:
err = C.vips_gifsave_go(img.VipsImage, &ptr, &imgsize)
case imagetype.HEIC:

View File

@@ -92,7 +92,7 @@ int vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int in
int vips_jxlsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort);
int vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize,
int colors);
int vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality);
int vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort, VipsForeignWebpPreset preset);
int vips_gifsave_go(VipsImage *in, void **buf, size_t *len);
int vips_heifsave_go(VipsImage *in, void **buf, size_t *len, int quality);
int vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed);