Переглянути джерело

Add origin width & height to debug headers

DarthSim 3 роки тому
батько
коміт
38ca14230d
3 змінених файлів з 35 додано та 2 видалено
  1. 3 0
      CHANGELOG.md
  2. 30 2
      processing/processing.go
  3. 2 0
      processing_handler.go

+ 3 - 0
CHANGELOG.md

@@ -1,6 +1,9 @@
 # Changelog
 
 ## [Unreleased]
+### Added
+- Add `X-Origin-Width` and `X-Origin-Height` to debug headers.
+
 ### Change
 - `dpr` processing option doesn't enlarge image unless `enlarge` is true.
 

+ 30 - 2
processing/processing.go

@@ -4,6 +4,7 @@ import (
 	"context"
 	"fmt"
 	"runtime"
+	"strconv"
 
 	log "github.com/sirupsen/logrus"
 
@@ -63,6 +64,16 @@ func canFitToBytes(imgtype imagetype.Type) bool {
 	}
 }
 
+func getImageSize(img *vips.Image) (int, int) {
+	width, height, _, _ := extractMeta(img, 0, true)
+
+	if pages, err := img.GetIntDefault("n-pages", 1); err != nil && pages > 0 {
+		height /= pages
+	}
+
+	return width, height
+}
+
 func transformAnimated(ctx context.Context, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
 	if po.Trim.Enabled {
 		log.Warning("Trim is not supported for animated images")
@@ -251,6 +262,8 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
 		return nil, err
 	}
 
+	originWidth, originHeight := getImageSize(img)
+
 	if animationSupport && img.IsAnimated() {
 		if err := transformAnimated(ctx, img, po, imgdata); err != nil {
 			return nil, err
@@ -265,9 +278,24 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
 		return nil, err
 	}
 
+	var (
+		outData *imagedata.ImageData
+		err     error
+	)
+
 	if po.MaxBytes > 0 && canFitToBytes(po.Format) {
-		return saveImageToFitBytes(ctx, po, img)
+		outData, err = saveImageToFitBytes(ctx, po, img)
+	} else {
+		outData, err = img.Save(po.Format, po.GetQuality())
+	}
+
+	if err == nil {
+		if outData.Headers == nil {
+			outData.Headers = make(map[string]string)
+		}
+		outData.Headers["X-Origin-Width"] = strconv.Itoa(originWidth)
+		outData.Headers["X-Origin-Height"] = strconv.Itoa(originHeight)
 	}
 
-	return img.Save(po.Format, po.GetQuality())
+	return outData, err
 }

+ 2 - 0
processing_handler.go

@@ -99,6 +99,8 @@ func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, res
 
 	if config.EnableDebugHeaders {
 		rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(originData.Data)))
+		rw.Header().Set("X-Origin-Width", resultData.Headers["X-Origin-Width"])
+		rw.Header().Set("X-Origin-Height", resultData.Headers["X-Origin-Height"])
 	}
 
 	rw.Header().Set("Content-Length", strconv.Itoa(len(resultData.Data)))