Ignore GIF when converting to AVIF (#302)

* Ignore GIF when converting to AVIF

* Allow WebP as source
This commit is contained in:
Nova Kwok 2023-12-01 20:26:27 +08:00 committed by GitHub
parent d67601af66
commit c771160a05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 21 deletions

View File

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

View File

@ -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,

View File

@ -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,