config.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package processing
  2. import (
  3. "errors"
  4. "fmt"
  5. "log/slog"
  6. "github.com/imgproxy/imgproxy/v3/config"
  7. "github.com/imgproxy/imgproxy/v3/ensure"
  8. "github.com/imgproxy/imgproxy/v3/imagetype"
  9. "github.com/imgproxy/imgproxy/v3/vips"
  10. )
  11. // Config holds pipeline-related configuration.
  12. type Config struct {
  13. PreferredFormats []imagetype.Type
  14. SkipProcessingFormats []imagetype.Type
  15. WatermarkOpacity float64
  16. DisableShrinkOnLoad bool
  17. UseLinearColorspace bool
  18. SanitizeSvg bool
  19. AlwaysRasterizeSvg bool
  20. Quality int
  21. FormatQuality map[imagetype.Type]int
  22. StripMetadata bool
  23. KeepCopyright bool
  24. StripColorProfile bool
  25. AutoRotate bool
  26. EnforceThumbnail bool
  27. }
  28. // NewConfig creates a new Config instance with the given parameters.
  29. func NewDefaultConfig() Config {
  30. return Config{
  31. WatermarkOpacity: 1,
  32. PreferredFormats: []imagetype.Type{
  33. imagetype.JPEG,
  34. imagetype.PNG,
  35. imagetype.GIF,
  36. },
  37. SanitizeSvg: true,
  38. Quality: 80,
  39. FormatQuality: map[imagetype.Type]int{
  40. imagetype.WEBP: 79,
  41. imagetype.AVIF: 63,
  42. imagetype.JXL: 77,
  43. },
  44. StripMetadata: true,
  45. KeepCopyright: true,
  46. StripColorProfile: true,
  47. AutoRotate: true,
  48. EnforceThumbnail: false,
  49. }
  50. }
  51. // NewConfig creates a new Config instance with the given parameters.
  52. func LoadConfigFromEnv(c *Config) (*Config, error) {
  53. c = ensure.Ensure(c, NewDefaultConfig)
  54. c.WatermarkOpacity = config.WatermarkOpacity
  55. c.DisableShrinkOnLoad = config.DisableShrinkOnLoad
  56. c.UseLinearColorspace = config.UseLinearColorspace
  57. c.SkipProcessingFormats = config.SkipProcessingFormats
  58. c.PreferredFormats = config.PreferredFormats
  59. c.SanitizeSvg = config.SanitizeSvg
  60. c.AlwaysRasterizeSvg = config.AlwaysRasterizeSvg
  61. c.AutoRotate = config.AutoRotate
  62. c.EnforceThumbnail = config.EnforceThumbnail
  63. return c, nil
  64. }
  65. // Validate checks if the configuration is valid
  66. func (c *Config) Validate() error {
  67. if c.WatermarkOpacity <= 0 {
  68. return errors.New("watermark opacity should be greater than 0")
  69. } else if c.WatermarkOpacity > 1 {
  70. return errors.New("watermark opacity should be less than or equal to 1")
  71. }
  72. filtered := c.PreferredFormats[:0]
  73. for _, t := range c.PreferredFormats {
  74. if !vips.SupportsSave(t) {
  75. slog.Warn(fmt.Sprintf("%s can't be a preferred format as it's saving is not supported", t))
  76. } else {
  77. filtered = append(filtered, t)
  78. }
  79. }
  80. if len(filtered) == 0 {
  81. return errors.New("no supported preferred formats specified")
  82. }
  83. c.PreferredFormats = filtered
  84. return nil
  85. }