Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avif support #206

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========
Expand Down
4 changes: 3 additions & 1 deletion engine/backend/goimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions engine/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var (
"jpg": "image/jpeg",
"png": "image/png",
"webp": "image/webp",
"avif": "image/avif",
}

MimeTypes = []string{
Expand All @@ -16,5 +17,6 @@ var (
"image/jpeg",
"image/png",
"image/webp",
"image/avif",
}
)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions image/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var (
"image/jpeg": "jpg",
"image/png": "png",
"image/webp": "webp",
"image/avif": "avif",
}

HeaderKeys = []string{
Expand Down
1 change: 1 addition & 0 deletions image/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ const (
TIFF
BMP
WEBP
AVIF
)
3 changes: 2 additions & 1 deletion parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var formats = map[string]image.Format{
"png": image.PNG,
"tiff": image.TIFF,
"webp": image.WEBP,
"avif": image.AVIF,
}

type Parameters struct {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (p *Processor) NewParameters(ctx context.Context, input *image.ImageFile, q
}

if format != input.Format() {
index := len(filepath) - len(input.Format())
index := len(filepath) - len(input.Format()) - 1

filepath = filepath[:index] + format

Expand Down
Loading