config.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package vips
  2. /*
  3. #include "vips.h"
  4. */
  5. import "C"
  6. import (
  7. "errors"
  8. "github.com/imgproxy/imgproxy/v3/ensure"
  9. "github.com/imgproxy/imgproxy/v3/env"
  10. )
  11. var (
  12. IMGPROXY_JPEG_PROGRESSIVE = env.Describe("IMGPROXY_JPEG_PROGRESSIVE", "boolean")
  13. IMGPROXY_PNG_INTERLACED = env.Describe("IMGPROXY_PNG_INTERLACED", "boolean")
  14. IMGPROXY_PNG_QUANTIZE = env.Describe("IMGPROXY_PNG_QUANTIZE", "boolean")
  15. IMGPROXY_PNG_QUANTIZATION_COLORS = env.Describe("IMGPROXY_PNG_QUANTIZATION_COLORS", "number between 2 and 256")
  16. IMGPROXY_WEBP_PRESET = env.Describe("IMGPROXY_WEBP_PRESET", "default|picture|photo|drawing|icon|text")
  17. IMGPROXY_AVIF_SPEED = env.Describe("IMGPROXY_AVIF_SPEED", "number between 0 and 9")
  18. IMGPROXY_WEBP_EFFORT = env.Describe("IMGPROXY_WEBP_EFFORT", "number between 1 and 6")
  19. IMGPROXY_JXL_EFFORT = env.Describe("IMGPROXY_JXL_EFFORT", "number between 1 and 9")
  20. IMGPROXY_PNG_UNLIMITED = env.Describe("IMGPROXY_PNG_UNLIMITED", "boolean")
  21. IMGPROXY_SVG_UNLIMITED = env.Describe("IMGPROXY_SVG_UNLIMITED", "boolean")
  22. IMGPROXY_VIPS_LEAK_CHECK = env.Describe("IMGPROXY_VIPS_LEAK_CHECK", "boolean")
  23. IMGPROXY_VIPS_CACHE_TRACE = env.Describe("IMGPROXY_VIPS_CACHE_TRACE", "boolean")
  24. )
  25. type Config struct {
  26. // Whether to save JPEG as progressive
  27. JpegProgressive bool
  28. // Whether to save PNG as interlaced
  29. PngInterlaced bool
  30. // Whether to save PNG with adaptive palette
  31. PngQuantize bool
  32. // Number of colors for adaptive palette
  33. PngQuantizationColors int
  34. // WebP preset to use when saving WebP images
  35. WebpPreset WebpPreset
  36. // AVIF saving speed
  37. AvifSpeed int
  38. // WebP saving effort
  39. WebpEffort int
  40. // JPEG XL saving effort
  41. JxlEffort int
  42. // Whether to not apply any limits when loading PNG
  43. PngUnlimited bool
  44. // Whether to not apply any limits when loading JPEG
  45. SvgUnlimited bool
  46. // Whether to enable libvips memory leak check
  47. LeakCheck bool
  48. // Whether to enable libvips operation cache tracing
  49. CacheTrace bool
  50. }
  51. func NewDefaultConfig() Config {
  52. return Config{
  53. JpegProgressive: false,
  54. PngInterlaced: false,
  55. PngQuantize: false,
  56. PngQuantizationColors: 256,
  57. WebpPreset: C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT,
  58. AvifSpeed: 8,
  59. WebpEffort: 4,
  60. JxlEffort: 4,
  61. PngUnlimited: false,
  62. SvgUnlimited: false,
  63. LeakCheck: false,
  64. CacheTrace: false,
  65. }
  66. }
  67. func LoadConfigFromEnv(c *Config) (*Config, error) {
  68. c = ensure.Ensure(c, NewDefaultConfig)
  69. var leakCheck, cacheTrace string
  70. // default preset so parsing below won't fail on empty value
  71. webpPreset := c.WebpPreset.String()
  72. err := errors.Join(
  73. env.Bool(&c.JpegProgressive, IMGPROXY_JPEG_PROGRESSIVE),
  74. env.Bool(&c.PngInterlaced, IMGPROXY_PNG_INTERLACED),
  75. env.Bool(&c.PngQuantize, IMGPROXY_PNG_QUANTIZE),
  76. env.Int(&c.PngQuantizationColors, IMGPROXY_PNG_QUANTIZATION_COLORS),
  77. env.Int(&c.AvifSpeed, IMGPROXY_AVIF_SPEED),
  78. env.Int(&c.WebpEffort, IMGPROXY_WEBP_EFFORT),
  79. env.Int(&c.JxlEffort, IMGPROXY_JXL_EFFORT),
  80. env.Bool(&c.PngUnlimited, IMGPROXY_PNG_UNLIMITED),
  81. env.Bool(&c.SvgUnlimited, IMGPROXY_SVG_UNLIMITED),
  82. env.String(&webpPreset, IMGPROXY_WEBP_PRESET),
  83. env.String(&leakCheck, IMGPROXY_VIPS_LEAK_CHECK),
  84. env.String(&cacheTrace, IMGPROXY_VIPS_CACHE_TRACE),
  85. )
  86. if err != nil {
  87. return nil, err
  88. }
  89. if pr, ok := WebpPresets[webpPreset]; ok {
  90. c.WebpPreset = pr
  91. } else {
  92. return nil, IMGPROXY_WEBP_PRESET.Errorf("invalid WebP preset: %s", webpPreset)
  93. }
  94. c.LeakCheck = len(leakCheck) > 0
  95. c.CacheTrace = len(cacheTrace) > 0
  96. return c, nil
  97. }
  98. func (c *Config) Validate() error {
  99. if c.PngQuantizationColors < 2 || c.PngQuantizationColors > 256 {
  100. return IMGPROXY_PNG_QUANTIZATION_COLORS.ErrorRange()
  101. }
  102. if c.WebpPreset < C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT || c.WebpPreset >= C.VIPS_FOREIGN_WEBP_PRESET_LAST {
  103. return IMGPROXY_WEBP_PRESET.ErrorRange()
  104. }
  105. if c.AvifSpeed < 0 || c.AvifSpeed > 9 {
  106. return IMGPROXY_AVIF_SPEED.ErrorRange()
  107. }
  108. if c.JxlEffort < 1 || c.JxlEffort > 9 {
  109. return IMGPROXY_JXL_EFFORT.ErrorRange()
  110. }
  111. if c.WebpEffort < 1 || c.WebpEffort > 6 {
  112. return IMGPROXY_WEBP_EFFORT.ErrorRange()
  113. }
  114. return nil
  115. }