Add origin width & height to debug headers

This commit is contained in:
DarthSim
2021-10-05 15:27:06 +06:00
parent b27f0d5712
commit 38ca14230d
3 changed files with 35 additions and 2 deletions

View File

@@ -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())
}
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
}