瀏覽代碼

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

hossein 1 月之前
父節點
當前提交
4d684fab0a
共有 4 個文件被更改,包括 46 次插入3 次删除
  1. 12 0
      config/config.go
  2. 3 1
      vips/vips.c
  3. 30 1
      vips/vips.go
  4. 1 1
      vips/vips.h

+ 12 - 0
config/config.go

@@ -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 {

+ 3 - 1
vips/vips.c

@@ -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);
 }
 

+ 30 - 1
vips/vips.go

@@ -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:

+ 1 - 1
vips/vips.h

@@ -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);