Move WebP enforcement/preference to processing

This commit is contained in:
DarthSim
2019-06-25 15:19:15 +06:00
parent c2ce5c5850
commit 7e979a9024
2 changed files with 12 additions and 8 deletions

View File

@@ -497,11 +497,15 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
imgtype := getImageType(ctx) imgtype := getImageType(ctx)
if po.Format == imageTypeUnknown { if po.Format == imageTypeUnknown {
if vipsTypeSupportSave[imgtype] { if po.PreferWebP && vipsTypeSupportSave[imageTypeWEBP] {
po.Format = imageTypeWEBP
} else if vipsTypeSupportSave[imgtype] {
po.Format = imgtype po.Format = imgtype
} else { } else {
po.Format = imageTypeJPEG po.Format = imageTypeJPEG
} }
} else if po.EnforceWebP && vipsTypeSupportSave[imageTypeWEBP] {
po.Format = imageTypeWEBP
} }
if !vipsSupportSmartcrop { if !vipsSupportSmartcrop {

View File

@@ -118,6 +118,9 @@ type processingOptions struct {
Watermark watermarkOptions Watermark watermarkOptions
PreferWebP bool
EnforceWebP bool
UsedPresets []string UsedPresets []string
} }
@@ -605,11 +608,6 @@ func applyFormatOption(po *processingOptions, args []string) error {
return fmt.Errorf("Invalid format arguments: %v", args) return fmt.Errorf("Invalid format arguments: %v", args)
} }
if conf.EnforceWebp && po.Format == imageTypeWEBP {
// Webp is enforced and already set as format
return nil
}
if f, ok := imageTypes[args[0]]; ok { if f, ok := imageTypes[args[0]]; ok {
po.Format = f po.Format = f
} else { } else {
@@ -769,9 +767,11 @@ func defaultProcessingOptions(headers *processingHeaders) (*processingOptions, e
UsedPresets: make([]string, 0, len(conf.Presets)), UsedPresets: make([]string, 0, len(conf.Presets)),
} }
if (conf.EnableWebpDetection || conf.EnforceWebp) && strings.Contains(headers.Accept, "image/webp") { if strings.Contains(headers.Accept, "image/webp") {
po.Format = imageTypeWEBP po.PreferWebP = conf.EnableWebpDetection || conf.EnforceWebp
po.EnforceWebP = conf.EnforceWebp
} }
if conf.EnableClientHints && len(headers.ViewportWidth) > 0 { if conf.EnableClientHints && len(headers.ViewportWidth) > 0 {
if vw, err := strconv.Atoi(headers.ViewportWidth); err == nil { if vw, err := strconv.Atoi(headers.ViewportWidth); err == nil {
po.Width = vw po.Width = vw