config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package vips
  2. /*
  3. #include "vips.h"
  4. */
  5. import "C"
  6. import (
  7. "fmt"
  8. "os"
  9. globalConfig "github.com/imgproxy/imgproxy/v3/config"
  10. "github.com/imgproxy/imgproxy/v3/ensure"
  11. )
  12. type Config struct {
  13. // Whether to save JPEG as progressive
  14. JpegProgressive bool
  15. // Whether to save PNG as interlaced
  16. PngInterlaced bool
  17. // Whether to save PNG with adaptive palette
  18. PngQuantize bool
  19. // Number of colors for adaptive palette
  20. PngQuantizationColors int
  21. // WebP preset to use when saving WebP images
  22. WebpPreset WebpPreset
  23. // AVIF saving speed
  24. AvifSpeed int
  25. // WebP saving effort
  26. WebpEffort int
  27. // JPEG XL saving effort
  28. JxlEffort int
  29. // Whether to not apply any limits when loading PNG
  30. PngUnlimited bool
  31. // Whether to not apply any limits when loading JPEG
  32. SvgUnlimited bool
  33. // Whether to enable libvips memory leak check
  34. LeakCheck bool
  35. // Whether to enable libvips operation cache tracing
  36. CacheTrace bool
  37. }
  38. func NewDefaultConfig() Config {
  39. return Config{
  40. JpegProgressive: false,
  41. PngInterlaced: false,
  42. PngQuantize: false,
  43. PngQuantizationColors: 256,
  44. WebpPreset: C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT,
  45. AvifSpeed: 8,
  46. WebpEffort: 4,
  47. JxlEffort: 4,
  48. PngUnlimited: false,
  49. SvgUnlimited: false,
  50. LeakCheck: false,
  51. CacheTrace: false,
  52. }
  53. }
  54. func LoadConfigFromEnv(c *Config) (*Config, error) {
  55. c = ensure.Ensure(c, NewDefaultConfig)
  56. c.JpegProgressive = globalConfig.JpegProgressive
  57. c.PngInterlaced = globalConfig.PngInterlaced
  58. c.PngQuantize = globalConfig.PngQuantize
  59. c.PngQuantizationColors = globalConfig.PngQuantizationColors
  60. if pr, ok := WebpPresets[globalConfig.WebpPreset]; ok {
  61. c.WebpPreset = pr
  62. } else {
  63. return nil, fmt.Errorf("invalid WebP preset: %s", globalConfig.WebpPreset)
  64. }
  65. c.AvifSpeed = globalConfig.AvifSpeed
  66. c.WebpEffort = globalConfig.WebpEffort
  67. c.JxlEffort = globalConfig.JxlEffort
  68. c.PngUnlimited = globalConfig.PngUnlimited
  69. c.SvgUnlimited = globalConfig.SvgUnlimited
  70. c.LeakCheck = len(os.Getenv("IMGPROXY_VIPS_LEAK_CHECK")) > 0
  71. c.CacheTrace = len(os.Getenv("IMGPROXY_VIPS_CACHE_TRACE")) > 0
  72. return c, nil
  73. }
  74. func (c *Config) Validate() error {
  75. if c.PngQuantizationColors < 2 || c.PngQuantizationColors > 256 {
  76. return fmt.Errorf(
  77. "IMGPROXY_PNG_QUANTIZATION_COLORS must be between 2 and 256, got %d",
  78. c.PngQuantizationColors,
  79. )
  80. }
  81. if c.WebpPreset < C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT || c.WebpPreset >= C.VIPS_FOREIGN_WEBP_PRESET_LAST {
  82. return fmt.Errorf("invalid IMGPROXY_WEBP_PRESET: %d", c.WebpPreset)
  83. }
  84. if c.AvifSpeed < 0 || c.AvifSpeed > 9 {
  85. return fmt.Errorf("IMGPROXY_AVIF_SPEED must be between 0 and 9, got %d", c.AvifSpeed)
  86. }
  87. if c.JxlEffort < 1 || c.JxlEffort > 9 {
  88. return fmt.Errorf("IMGPROXY_JXL_EFFORT must be between 1 and 9, got %d", c.JxlEffort)
  89. }
  90. if c.WebpEffort < 1 || c.WebpEffort > 6 {
  91. return fmt.Errorf("IMGPROXY_WEBP_EFFORT must be between 1 and 6, got %d", c.WebpEffort)
  92. }
  93. return nil
  94. }