mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Ignore GIF when converting to AVIF (#302)
* Ignore GIF when converting to AVIF * Allow WebP as source
This commit is contained in:
parent
d67601af66
commit
c771160a05
@ -12,9 +12,11 @@
|
|||||||
|
|
||||||
This is a Server based on Golang, which allows you to serve WebP images on the fly.
|
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)
|
## Usage with Docker(recommended)
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func NewWebPConfig() *WebpConfig {
|
|||||||
Port: "3333",
|
Port: "3333",
|
||||||
ImgPath: "./pics",
|
ImgPath: "./pics",
|
||||||
Quality: 80,
|
Quality: 80,
|
||||||
AllowedTypes: []string{"jpg", "png", "jpeg", "bmp", "svg", "nef"},
|
AllowedTypes: []string{"jpg", "png", "jpeg", "bmp", "svg", "nef", "heic", "webp"},
|
||||||
ImageMap: map[string]string{},
|
ImageMap: map[string]string{},
|
||||||
ExhaustPath: "./exhaust",
|
ExhaustPath: "./exhaust",
|
||||||
EnableAVIF: false,
|
EnableAVIF: false,
|
||||||
|
@ -17,6 +17,11 @@ import (
|
|||||||
var (
|
var (
|
||||||
boolFalse vips.BoolParameter
|
boolFalse vips.BoolParameter
|
||||||
intMinusOne vips.IntParameter
|
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() {
|
func init() {
|
||||||
@ -135,18 +140,6 @@ func convertImage(raw, optimized, imageType string, extraParams config.ExtraPara
|
|||||||
return err
|
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 {
|
func avifEncoder(p1, p2 string, extraParams config.ExtraParams) error {
|
||||||
// if convert fails, return error; success nil
|
// if convert fails, return error; success nil
|
||||||
var (
|
var (
|
||||||
@ -160,8 +153,12 @@ func avifEncoder(p1, p2 string, extraParams config.ExtraParams) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if imageIgnore(img.Format()) {
|
imageFormat := img.Format()
|
||||||
return errors.New("encoder: ignore image type")
|
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 {
|
if config.Config.EnableExtraParams {
|
||||||
@ -225,10 +222,13 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if imageIgnore(img.Format()) {
|
imageFormat := img.Format()
|
||||||
return errors.New("encoder: ignore image type")
|
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 {
|
if config.Config.EnableExtraParams {
|
||||||
err = resizeImage(img, extraParams)
|
err = resizeImage(img, extraParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -256,7 +256,7 @@ func webpEncoder(p1, p2 string, extraParams config.ExtraParams) error {
|
|||||||
StripMetadata: true,
|
StripMetadata: true,
|
||||||
})
|
})
|
||||||
} else {
|
} 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
|
// Example: https://github.com/webp-sh/webp_server_go/issues/234
|
||||||
ep := vips.WebpExportParams{
|
ep := vips.WebpExportParams{
|
||||||
Quality: quality,
|
Quality: quality,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user