|
@@ -54,14 +54,14 @@ type TrimOptions struct {
|
|
|
}
|
|
|
|
|
|
type WatermarkOptions struct {
|
|
|
- Enabled bool
|
|
|
- Opacity float64
|
|
|
- Gravity GravityOptions
|
|
|
- Scale float64
|
|
|
+ Enabled bool
|
|
|
+ Opacity float64
|
|
|
+ Position GravityOptions
|
|
|
+ Scale float64
|
|
|
}
|
|
|
|
|
|
func (wo WatermarkOptions) ShouldReplicate() bool {
|
|
|
- return wo.Gravity.Type == GravityReplicate
|
|
|
+ return wo.Position.Type == GravityReplicate
|
|
|
}
|
|
|
|
|
|
type ProcessingOptions struct {
|
|
@@ -142,7 +142,7 @@ func NewProcessingOptions() *ProcessingOptions {
|
|
|
Blur: 0,
|
|
|
Sharpen: 0,
|
|
|
Dpr: 1,
|
|
|
- Watermark: WatermarkOptions{Opacity: 1, Gravity: GravityOptions{Type: GravityCenter}},
|
|
|
+ Watermark: WatermarkOptions{Opacity: 1, Position: GravityOptions{Type: GravityCenter}},
|
|
|
StripMetadata: config.StripMetadata,
|
|
|
KeepCopyright: config.KeepCopyright,
|
|
|
StripColorProfile: config.StripColorProfile,
|
|
@@ -217,14 +217,14 @@ func isGravityOffcetValid(gravity GravityType, offset float64) bool {
|
|
|
return gravity != GravityFocusPoint || (offset >= 0 && offset <= 1)
|
|
|
}
|
|
|
|
|
|
-func parseGravity(g *GravityOptions, args []string) error {
|
|
|
+func parseGravity(g *GravityOptions, args []string, allowedTypes []GravityType) error {
|
|
|
nArgs := len(args)
|
|
|
|
|
|
if nArgs > 3 {
|
|
|
return fmt.Errorf("Invalid gravity arguments: %v", args)
|
|
|
}
|
|
|
|
|
|
- if t, ok := gravityTypes[args[0]]; ok {
|
|
|
+ if t, ok := gravityTypes[args[0]]; ok && slices.Contains(allowedTypes, t) {
|
|
|
g.Type = t
|
|
|
} else {
|
|
|
return fmt.Errorf("Invalid gravity: %s", args[0])
|
|
@@ -263,13 +263,9 @@ func parseExtend(opts *ExtendOptions, name string, args []string) error {
|
|
|
opts.Enabled = parseBoolOption(args[0])
|
|
|
|
|
|
if len(args) > 1 {
|
|
|
- if err := parseGravity(&opts.Gravity, args[1:]); err != nil {
|
|
|
+ if err := parseGravity(&opts.Gravity, args[1:], extendGravityTypes); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
-
|
|
|
- if !opts.Gravity.Type.OkForExtend() {
|
|
|
- return fmt.Errorf("%s doesn't support %s gravity", name, opts.Gravity.Type)
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
return nil
|
|
@@ -431,14 +427,10 @@ func applyDprOption(po *ProcessingOptions, args []string) error {
|
|
|
}
|
|
|
|
|
|
func applyGravityOption(po *ProcessingOptions, args []string) error {
|
|
|
- if err := parseGravity(&po.Gravity, args); err != nil {
|
|
|
+ if err := parseGravity(&po.Gravity, args, cropGravityTypes); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
- if !po.Gravity.Type.OkForCrop() {
|
|
|
- return fmt.Errorf("%s gravity type is not applicable to gravity", po.Gravity.Type)
|
|
|
- }
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -462,12 +454,9 @@ func applyCropOption(po *ProcessingOptions, args []string) error {
|
|
|
}
|
|
|
|
|
|
if len(args) > 2 {
|
|
|
- if err := parseGravity(&po.Crop.Gravity, args[2:]); err != nil {
|
|
|
+ if err := parseGravity(&po.Crop.Gravity, args[2:], cropGravityTypes); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- if !po.Crop.Gravity.Type.OkForCrop() {
|
|
|
- return fmt.Errorf("%s gravity type is not applicable to crop", po.Crop.Gravity.Type)
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
return nil
|
|
@@ -731,8 +720,8 @@ func applyWatermarkOption(po *ProcessingOptions, args []string) error {
|
|
|
}
|
|
|
|
|
|
if len(args) > 1 && len(args[1]) > 0 {
|
|
|
- if g, ok := gravityTypes[args[1]]; ok && g.OkForWatermark() {
|
|
|
- po.Watermark.Gravity.Type = g
|
|
|
+ if g, ok := gravityTypes[args[1]]; ok && slices.Contains(watermarkGravityTypes, g) {
|
|
|
+ po.Watermark.Position.Type = g
|
|
|
} else {
|
|
|
return fmt.Errorf("Invalid watermark position: %s", args[1])
|
|
|
}
|
|
@@ -740,7 +729,7 @@ func applyWatermarkOption(po *ProcessingOptions, args []string) error {
|
|
|
|
|
|
if len(args) > 2 && len(args[2]) > 0 {
|
|
|
if x, err := strconv.ParseFloat(args[2], 64); err == nil {
|
|
|
- po.Watermark.Gravity.X = x
|
|
|
+ po.Watermark.Position.X = x
|
|
|
} else {
|
|
|
return fmt.Errorf("Invalid watermark X offset: %s", args[2])
|
|
|
}
|
|
@@ -748,7 +737,7 @@ func applyWatermarkOption(po *ProcessingOptions, args []string) error {
|
|
|
|
|
|
if len(args) > 3 && len(args[3]) > 0 {
|
|
|
if y, err := strconv.ParseFloat(args[3], 64); err == nil {
|
|
|
- po.Watermark.Gravity.Y = y
|
|
|
+ po.Watermark.Position.Y = y
|
|
|
} else {
|
|
|
return fmt.Errorf("Invalid watermark Y offset: %s", args[3])
|
|
|
}
|