From c771160a05295e3c733ec9bf9dad4025b01ae28c Mon Sep 17 00:00:00 2001 From: Nova Kwok Date: Fri, 1 Dec 2023 20:26:27 +0800 Subject: [PATCH] Ignore GIF when converting to AVIF (#302) * Ignore GIF when converting to AVIF * Allow WebP as source --- README.md | 6 ++++-- config/config.go | 2 +- encoder/encoder.go | 36 ++++++++++++++++++------------------ 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 5eba22b6b..52f157411 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,11 @@ This is a Server based on Golang, which allows you to serve WebP images on the fly. -Currently supported image format: JPEG, PNG, BMP, GIF, SVG, HEIC, NEF +Currently supported image format: JPEG, PNG, BMP, GIF, SVG, HEIC, NEF, WEBP -> e.g When you visit `https://your.website/pics/tsuki.jpg`,it will serve as `image/webp` format without changing the URL. +> e.g When you visit `https://your.website/pics/tsuki.jpg`,it will serve as `image/webp`/`image/avif` format without changing the URL. +> +> GIF image will not be converted to AVIF format even with `ENABLE_AVIF` to `true`, because the converted AVIF image is not animated. ## Usage with Docker(recommended) diff --git a/config/config.go b/config/config.go index 7bf2881af..e501c5159 100644 --- a/config/config.go +++ b/config/config.go @@ -80,7 +80,7 @@ func NewWebPConfig() *WebpConfig { Port: "3333", ImgPath: "./pics", Quality: 80, - AllowedTypes: []string{"jpg", "png", "jpeg", "bmp", "svg", "nef"}, + AllowedTypes: []string{"jpg", "png", "jpeg", "bmp", "svg", "nef", "heic", "webp"}, ImageMap: map[string]string{}, ExhaustPath: "./exhaust", EnableAVIF: false, diff --git a/encoder/encoder.go b/encoder/encoder.go index cf9e15035..d9d03b388 100644 --- a/encoder/encoder.go +++ b/encoder/encoder.go @@ -17,6 +17,11 @@ import ( var ( boolFalse vips.BoolParameter intMinusOne vips.IntParameter + // Source image encoder ignore list for WebP and AVIF + // We shouldn't convert Unknown and AVIF to WebP + webpIgnore = []vips.ImageType{vips.ImageTypeUnknown, vips.ImageTypeAVIF} + // We shouldn't convert Unknown,AVIF and GIF to AVIF + avifIgnore = append(webpIgnore, vips.ImageTypeGIF) ) func init() { @@ -135,18 +140,6 @@ func convertImage(raw, optimized, imageType string, extraParams config.ExtraPara return err } -func imageIgnore(imageFormat vips.ImageType) bool { - // Ignore Unknown, WebP, AVIF - ignoreList := []vips.ImageType{vips.ImageTypeUnknown, vips.ImageTypeWEBP, vips.ImageTypeAVIF} - for _, ignore := range ignoreList { - if imageFormat == ignore { - // Return err to render original image - return true - } - } - return false -} - func avifEncoder(p1, p2 string, extraParams config.ExtraParams) error { // if convert fails, return error; success nil var ( @@ -160,8 +153,12 @@ func avifEncoder(p1, p2 string, extraParams config.ExtraParams) error { return err } - if imageIgnore(img.Format()) { - return errors.New("encoder: ignore image type") + imageFormat := img.Format() + for _, ignore := range avifIgnore { + if imageFormat == ignore { + // Return err to render original image + return errors.New("AVIF encoder: ignore image type") + } } if config.Config.EnableExtraParams { @@ -225,10 +222,13 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error { return err } - if imageIgnore(img.Format()) { - return errors.New("encoder: ignore image type") + imageFormat := img.Format() + for _, ignore := range webpIgnore { + if imageFormat == ignore { + // Return err to render original image + return errors.New("WebP encoder: ignore image type") + } } - if config.Config.EnableExtraParams { err = resizeImage(img, extraParams) if err != nil { @@ -256,7 +256,7 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error { StripMetadata: true, }) } else { - // If some special images cannot encode with default ReductionEffort(0), then try with 4 + // If some special images cannot encode with default ReductionEffort(0), then retry from 0 to 6 // Example: https://github.com/webp-sh/webp_server_go/issues/234 ep := vips.WebpExportParams{ Quality: quality,