mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-10 12:12:40 +02:00
99 lines
2.5 KiB
Go
99 lines
2.5 KiB
Go
package processing
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/imgproxy/imgproxy/v3/config"
|
|
"github.com/imgproxy/imgproxy/v3/ensure"
|
|
"github.com/imgproxy/imgproxy/v3/imagetype"
|
|
"github.com/imgproxy/imgproxy/v3/vips"
|
|
)
|
|
|
|
// Config holds pipeline-related configuration.
|
|
type Config struct {
|
|
PreferredFormats []imagetype.Type
|
|
SkipProcessingFormats []imagetype.Type
|
|
WatermarkOpacity float64
|
|
DisableShrinkOnLoad bool
|
|
UseLinearColorspace bool
|
|
SanitizeSvg bool
|
|
AlwaysRasterizeSvg bool
|
|
Quality int
|
|
FormatQuality map[imagetype.Type]int
|
|
StripMetadata bool
|
|
KeepCopyright bool
|
|
StripColorProfile bool
|
|
AutoRotate bool
|
|
EnforceThumbnail bool
|
|
}
|
|
|
|
// NewConfig creates a new Config instance with the given parameters.
|
|
func NewDefaultConfig() Config {
|
|
return Config{
|
|
WatermarkOpacity: 1,
|
|
PreferredFormats: []imagetype.Type{
|
|
imagetype.JPEG,
|
|
imagetype.PNG,
|
|
imagetype.GIF,
|
|
},
|
|
SanitizeSvg: true,
|
|
Quality: 80,
|
|
FormatQuality: map[imagetype.Type]int{
|
|
imagetype.WEBP: 79,
|
|
imagetype.AVIF: 63,
|
|
imagetype.JXL: 77,
|
|
},
|
|
StripMetadata: true,
|
|
KeepCopyright: true,
|
|
StripColorProfile: true,
|
|
AutoRotate: true,
|
|
EnforceThumbnail: false,
|
|
}
|
|
}
|
|
|
|
// NewConfig creates a new Config instance with the given parameters.
|
|
func LoadConfigFromEnv(c *Config) (*Config, error) {
|
|
c = ensure.Ensure(c, NewDefaultConfig)
|
|
|
|
c.WatermarkOpacity = config.WatermarkOpacity
|
|
c.DisableShrinkOnLoad = config.DisableShrinkOnLoad
|
|
c.UseLinearColorspace = config.UseLinearColorspace
|
|
c.SkipProcessingFormats = config.SkipProcessingFormats
|
|
c.PreferredFormats = config.PreferredFormats
|
|
c.SanitizeSvg = config.SanitizeSvg
|
|
c.AlwaysRasterizeSvg = config.AlwaysRasterizeSvg
|
|
c.AutoRotate = config.AutoRotate
|
|
c.EnforceThumbnail = config.EnforceThumbnail
|
|
|
|
return c, nil
|
|
}
|
|
|
|
// Validate checks if the configuration is valid
|
|
func (c *Config) Validate() error {
|
|
if c.WatermarkOpacity <= 0 {
|
|
return errors.New("watermark opacity should be greater than 0")
|
|
} else if c.WatermarkOpacity > 1 {
|
|
return errors.New("watermark opacity should be less than or equal to 1")
|
|
}
|
|
|
|
filtered := c.PreferredFormats[:0]
|
|
|
|
for _, t := range c.PreferredFormats {
|
|
if !vips.SupportsSave(t) {
|
|
slog.Warn(fmt.Sprintf("%s can't be a preferred format as it's saving is not supported", t))
|
|
} else {
|
|
filtered = append(filtered, t)
|
|
}
|
|
}
|
|
|
|
if len(filtered) == 0 {
|
|
return errors.New("no supported preferred formats specified")
|
|
}
|
|
|
|
c.PreferredFormats = filtered
|
|
|
|
return nil
|
|
}
|