From df8423e38707c17d994b4692b5dce3b5c98657b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Lauko?= Date: Fri, 3 Jan 2025 15:47:46 +0100 Subject: [PATCH] avif support --- README.rst | 1 + engine/backend/goimage.go | 4 +++- engine/constants.go | 2 ++ go.mod | 3 +++ go.sum | 6 ++++++ image/constants.go | 1 + image/formats.go | 1 + 7 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a7389cf5..5f360ecd 100644 --- a/README.rst +++ b/README.rst @@ -432,6 +432,7 @@ picfit currently supports the following image formats: - ``image/gif`` with the keyword ``gif`` - ``image/bmp`` with the keyword ``bmp`` - ``image/webp`` with the keyword ``webp`` +- ``image/avif`` with the keyword ``avif`` Operations ========== diff --git a/engine/backend/goimage.go b/engine/backend/goimage.go index a6005cdc..fba98460 100644 --- a/engine/backend/goimage.go +++ b/engine/backend/goimage.go @@ -21,6 +21,7 @@ import ( imagefile "github.com/thoas/picfit/image" "github.com/chai2010/webp" + "github.com/gen2brain/avif" "golang.org/x/image/bmp" "golang.org/x/image/tiff" ) @@ -106,7 +107,6 @@ func (e *GoImage) Fit(ctx context.Context, img *imagefile.ImageFile, options *Op return e.transform(image, options, imaging.Fit) } - func (e *GoImage) Effect(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) { image, err := e.source(img) if err != nil { @@ -274,6 +274,8 @@ func encode(w io.Writer, img image.Image, format imagefile.Format, quality int) err = bmp.Encode(w, img) case imagefile.WEBP: err = webp.Encode(w, img, &webp.Options{Quality: float32(quality)}) + case imagefile.AVIF: + err = avif.Encode(w, img, avif.Options{Quality: quality}) default: err = imaging.ErrUnsupportedFormat } diff --git a/engine/constants.go b/engine/constants.go index 58b3e88d..c91b8f56 100644 --- a/engine/constants.go +++ b/engine/constants.go @@ -8,6 +8,7 @@ var ( "jpg": "image/jpeg", "png": "image/png", "webp": "image/webp", + "avif": "image/avif", } MimeTypes = []string{ @@ -16,5 +17,6 @@ var ( "image/jpeg", "image/png", "image/webp", + "image/avif", } ) diff --git a/go.mod b/go.mod index bdfec571..54db83ce 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( require ( github.com/chai2010/webp v1.1.1 + github.com/gen2brain/avif v0.4.2 github.com/google/uuid v1.3.0 github.com/prometheus/client_golang v1.14.0 golang.org/x/sync v0.7.0 @@ -51,6 +52,7 @@ require ( github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/ebitengine/purego v0.8.1 // indirect github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect @@ -88,6 +90,7 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.2.0 // indirect + github.com/tetratelabs/wazero v1.8.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.11 // indirect go.opencensus.io v0.24.0 // indirect diff --git a/go.sum b/go.sum index 29901518..ece49873 100644 --- a/go.sum +++ b/go.sum @@ -106,6 +106,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/ebitengine/purego v0.8.1 h1:sdRKd6plj7KYW33EH5As6YKfe8m9zbN9JMrOjNVF/BE= +github.com/ebitengine/purego v0.8.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= @@ -119,6 +121,8 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= +github.com/gen2brain/avif v0.4.2 h1:rOZklPjZg3qTvKw/oR4xbdAe2JxvJGdFsGltnYmn2Mo= +github.com/gen2brain/avif v0.4.2/go.mod h1:oePci7KPleKZ8X/2rjZ3FlVm2JFYjPwXiQpNgq9wrzs= github.com/getsentry/sentry-go v0.19.0 h1:BcCH3CN5tXt5aML+gwmbFwVptLLQA+eT866fCO9wVOM= github.com/getsentry/sentry-go v0.19.0/go.mod h1:y3+lGEFEFexZtpbG1GUE2WD/f9zGyKYwpEqryTOC/nE= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -445,6 +449,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/tetratelabs/wazero v1.8.1 h1:NrcgVbWfkWvVc4UtT4LRLDf91PsOzDzefMdwhLfA550= +github.com/tetratelabs/wazero v1.8.1/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw= github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= github.com/thoas/stats v0.0.0-20160726120248-152b5d051953 h1:PD6HdaGc9tn2a8W/33zxcA6DTq1D1K0O/PKWOGz3Lxo= diff --git a/image/constants.go b/image/constants.go index 29257b8a..75e33326 100644 --- a/image/constants.go +++ b/image/constants.go @@ -7,6 +7,7 @@ var ( "image/jpeg": "jpg", "image/png": "png", "image/webp": "webp", + "image/avif": "avif", } HeaderKeys = []string{ diff --git a/image/formats.go b/image/formats.go index d5831ee1..c20d4d55 100644 --- a/image/formats.go +++ b/image/formats.go @@ -10,4 +10,5 @@ const ( TIFF BMP WEBP + AVIF )