Bladeren bron

Fix Client Hints behavior

DarthSim 3 jaren geleden
bovenliggende
commit
da10f3eac3
3 gewijzigde bestanden met toevoegingen van 13 en 6 verwijderingen
  1. 2 0
      CHANGELOG.md
  2. 7 6
      options/processing_options.go
  3. 4 0
      processing_handler.go

+ 2 - 0
CHANGELOG.md

@@ -1,6 +1,8 @@
 # Changelog
 
 ## [Unreleased]
+### Fix
+- Fix Client Hints behavior. `Width` is physical size, so we should divide it by `DPR` value.
 
 ## [3.0.0.beta1] - 2021-10-01
 ### Added

+ 7 - 6
options/processing_options.go

@@ -14,6 +14,7 @@ import (
 	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/ierrors"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
+	"github.com/imgproxy/imgproxy/v3/imath"
 	"github.com/imgproxy/imgproxy/v3/structdiff"
 	"github.com/imgproxy/imgproxy/v3/vips"
 )
@@ -914,6 +915,11 @@ func defaultProcessingOptions(headers http.Header) (*ProcessingOptions, error) {
 	}
 
 	if config.EnableClientHints {
+		if headerDPR := headers.Get("DPR"); len(headerDPR) > 0 {
+			if dpr, err := strconv.ParseFloat(headerDPR, 64); err == nil && (dpr > 0 && dpr <= maxClientHintDPR) {
+				po.Dpr = dpr
+			}
+		}
 		if headerViewportWidth := headers.Get("Viewport-Width"); len(headerViewportWidth) > 0 {
 			if vw, err := strconv.Atoi(headerViewportWidth); err == nil {
 				po.Width = vw
@@ -921,12 +927,7 @@ func defaultProcessingOptions(headers http.Header) (*ProcessingOptions, error) {
 		}
 		if headerWidth := headers.Get("Width"); len(headerWidth) > 0 {
 			if w, err := strconv.Atoi(headerWidth); err == nil {
-				po.Width = w
-			}
-		}
-		if headerDPR := headers.Get("DPR"); len(headerDPR) > 0 {
-			if dpr, err := strconv.ParseFloat(headerDPR, 64); err == nil && (dpr > 0 && dpr <= maxClientHintDPR) {
-				po.Dpr = dpr
+				po.Width = imath.Scale(w, 1/po.Dpr)
 			}
 		}
 	}

+ 4 - 0
processing_handler.go

@@ -59,6 +59,10 @@ func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, res
 	rw.Header().Set("Content-Type", resultData.Type.Mime())
 	rw.Header().Set("Content-Disposition", contentDisposition)
 
+	if po.Dpr != 1 {
+		rw.Header().Set("Content-DPR", strconv.FormatFloat(po.Dpr, 'f', 2, 32))
+	}
+
 	if config.SetCanonicalHeader {
 		if strings.HasPrefix(originURL, "https://") || strings.HasPrefix(originURL, "http://") {
 			linkHeader := fmt.Sprintf(`<%s>; rel="canonical"`, originURL)