mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-11 04:32:29 +02:00
IMGPROXY_PREFERRED_FORMATS config
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user