IMGPROXY_PREFERRED_FORMATS config

This commit is contained in:
DarthSim
2022-07-18 17:49:04 +06:00
parent 9adf06fb04
commit f368ed13e9
6 changed files with 82 additions and 8 deletions

View File

@@ -37,9 +37,40 @@ var mainPipeline = pipeline{
finalize,
}
func imageTypeGoodForWeb(imgtype imagetype.Type) bool {
return imgtype != imagetype.TIFF &&
imgtype != imagetype.BMP
func isImageTypePreferred(imgtype imagetype.Type) bool {
for _, t := range config.PreferredFormats {
if imgtype == t {
return true
}
}
return false
}
func findBestFormat(srcType imagetype.Type, animated, expectAlpha bool) imagetype.Type {
for _, t := range config.PreferredFormats {
if animated && !t.SupportsAnimation() {
continue
}
if expectAlpha && !t.SupportsAlpha() {
continue
}
return t
}
return config.PreferredFormats[0]
}
func ValidatePreferredFormats() error {
for _, t := range config.PreferredFormats {
if !vips.SupportsSave(t) {
return fmt.Errorf("%s can't be a preferred format as it's saving is not supported", t)
}
}
return nil
}
func canFitToBytes(imgtype imagetype.Type) bool {
@@ -223,6 +254,7 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
originWidth, originHeight := getImageSize(img)
animated := img.IsAnimated()
expectAlpha := !po.Flatten && (img.HasAlpha() || po.Padding.Enabled || po.Extend.Enabled)
switch {
case po.Format == imagetype.Unknown:
@@ -231,10 +263,10 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
po.Format = imagetype.AVIF
case po.PreferWebP:
po.Format = imagetype.WEBP
case vips.SupportsSave(imgdata.Type) && imageTypeGoodForWeb(imgdata.Type):
case isImageTypePreferred(imgdata.Type):
po.Format = imgdata.Type
default:
po.Format = imagetype.JPEG
po.Format = findBestFormat(imgdata.Type, animated, expectAlpha)
}
case po.EnforceAvif && !animated:
po.Format = imagetype.AVIF