|
@@ -37,9 +37,40 @@ var mainPipeline = pipeline{
|
|
finalize,
|
|
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 {
|
|
func canFitToBytes(imgtype imagetype.Type) bool {
|
|
@@ -223,6 +254,7 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
|
|
originWidth, originHeight := getImageSize(img)
|
|
originWidth, originHeight := getImageSize(img)
|
|
|
|
|
|
animated := img.IsAnimated()
|
|
animated := img.IsAnimated()
|
|
|
|
+ expectAlpha := !po.Flatten && (img.HasAlpha() || po.Padding.Enabled || po.Extend.Enabled)
|
|
|
|
|
|
switch {
|
|
switch {
|
|
case po.Format == imagetype.Unknown:
|
|
case po.Format == imagetype.Unknown:
|
|
@@ -231,10 +263,10 @@ func ProcessImage(ctx context.Context, imgdata *imagedata.ImageData, po *options
|
|
po.Format = imagetype.AVIF
|
|
po.Format = imagetype.AVIF
|
|
case po.PreferWebP:
|
|
case po.PreferWebP:
|
|
po.Format = imagetype.WEBP
|
|
po.Format = imagetype.WEBP
|
|
- case vips.SupportsSave(imgdata.Type) && imageTypeGoodForWeb(imgdata.Type):
|
|
|
|
|
|
+ case isImageTypePreferred(imgdata.Type):
|
|
po.Format = imgdata.Type
|
|
po.Format = imgdata.Type
|
|
default:
|
|
default:
|
|
- po.Format = imagetype.JPEG
|
|
|
|
|
|
+ po.Format = findBestFormat(imgdata.Type, animated, expectAlpha)
|
|
}
|
|
}
|
|
case po.EnforceAvif && !animated:
|
|
case po.EnforceAvif && !animated:
|
|
po.Format = imagetype.AVIF
|
|
po.Format = imagetype.AVIF
|