You've already forked imgproxy
mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-12-07 23:32:55 +02:00
Add origin width & height to debug headers
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Added
|
||||||
|
- Add `X-Origin-Width` and `X-Origin-Height` to debug headers.
|
||||||
|
|
||||||
### Change
|
### Change
|
||||||
- `dpr` processing option doesn't enlarge image unless `enlarge` is true.
|
- `dpr` processing option doesn't enlarge image unless `enlarge` is true.
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
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 {
|
func transformAnimated(ctx context.Context, img *vips.Image, po *options.ProcessingOptions, imgdata *imagedata.ImageData) error {
|
||||||
if po.Trim.Enabled {
|
if po.Trim.Enabled {
|
||||||
log.Warning("Trim is not supported for animated images")
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
originWidth, originHeight := getImageSize(img)
|
||||||
|
|
||||||
if animationSupport && img.IsAnimated() {
|
if animationSupport && img.IsAnimated() {
|
||||||
if err := transformAnimated(ctx, img, po, imgdata); err != nil {
|
if err := transformAnimated(ctx, img, po, imgdata); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -265,9 +278,24 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
outData *imagedata.ImageData
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
if po.MaxBytes > 0 && canFitToBytes(po.Format) {
|
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())
|
||||||
}
|
}
|
||||||
|
|
||||||
return 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 outData, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,8 @@ func respondWithImage(reqID string, r *http.Request, rw http.ResponseWriter, res
|
|||||||
|
|
||||||
if config.EnableDebugHeaders {
|
if config.EnableDebugHeaders {
|
||||||
rw.Header().Set("X-Origin-Content-Length", strconv.Itoa(len(originData.Data)))
|
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)))
|
rw.Header().Set("Content-Length", strconv.Itoa(len(resultData.Data)))
|
||||||
|
|||||||
Reference in New Issue
Block a user