Skip to content

Commit

Permalink
Ignore GIF when converting to AVIF (#302)
Browse files Browse the repository at this point in the history
* Ignore GIF when converting to AVIF

* Allow WebP as source
  • Loading branch information
n0vad3v authored Dec 1, 2023
1 parent d67601a commit c771160
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 18 additions & 18 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 (
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c771160

Please sign in to comment.