Skip to content

Commit

Permalink
webp support (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas authored Oct 14, 2024
1 parent 7019f5b commit f684e81
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 20 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ build:
-X github.com/thoas/picfit/constants.Branch=$(branch) \
-X github.com/thoas/picfit/constants.Revision=$(commit) \
-X 'github.com/thoas/picfit/constants.BuildTime=$(now)' \
-X 'github.com/thoas/picfit/constants.LatestCommitMessage=$(commitMessage)' \
-X 'github.com/thoas/picfit/constants.Compiler=$(compiler)'" -o $(BIN_DIR)/picfit ./cmd/picfit/main.go
@(echo "-> picfit binary created")

Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ picfit currently supports the following image formats:
- ``image/png`` with the keyword ``png``
- ``image/gif`` with the keyword ``gif``
- ``image/bmp`` with the keyword ``bmp``
- ``image/webp`` with the keyword ``webp``

Operations
==========
Expand Down
3 changes: 1 addition & 2 deletions engine/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/go-spectest/imaging"
"github.com/pkg/errors"
"github.com/thoas/picfit/image"
)
Expand All @@ -17,7 +16,7 @@ type Options struct {
Color string
Degree int
Filter string
Format imaging.Format
Format image.Format
Height int
Images []image.ImageFile
Position string
Expand Down
26 changes: 17 additions & 9 deletions engine/backend/goimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

imagefile "github.com/thoas/picfit/image"

"github.com/chai2010/webp"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)
Expand Down Expand Up @@ -88,7 +89,7 @@ func (e *GoImage) Flip(ctx context.Context, img *imagefile.ImageFile, options *O
}

func (e *GoImage) Fit(ctx context.Context, img *imagefile.ImageFile, options *Options) ([]byte, error) {
if options.Format == imaging.GIF {
if options.Format == imagefile.GIF {
content, err := e.transformGIF(img, options, imaging.Thumbnail)
if err != nil {
return nil, err
Expand All @@ -105,6 +106,7 @@ 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 All @@ -122,7 +124,7 @@ func (e *GoImage) Effect(ctx context.Context, img *imagefile.ImageFile, options
return nil, MethodNotImplementedError
}

func (e *GoImage) toBytes(img image.Image, format imaging.Format, quality int) ([]byte, error) {
func (e *GoImage) toBytes(img image.Image, format imagefile.Format, quality int) ([]byte, error) {
var buf bytes.Buffer

if err := encode(&buf, img, format, quality); err != nil {
Expand Down Expand Up @@ -182,7 +184,7 @@ func (e *GoImage) transformGIF(img *imagefile.ImageFile, options *Options, trans
}

func (e *GoImage) resize(img *imagefile.ImageFile, options *Options, trans transformation) ([]byte, error) {
if options.Format == imaging.GIF {
if options.Format == imagefile.GIF {
content, err := e.transformGIF(img, options, trans)
if err != nil {
return nil, err
Expand All @@ -200,6 +202,10 @@ func (e *GoImage) resize(img *imagefile.ImageFile, options *Options, trans trans
}

func (e *GoImage) transform(img image.Image, options *Options, trans transformation) ([]byte, error) {
if options.Height == 0 && options.Width == 0 {
return e.toBytes(img, options.Format, options.Quality)
}

return e.toBytes(scale(img, options, trans), options.Format, options.Quality)
}

Expand Down Expand Up @@ -238,10 +244,10 @@ func imageToPaletted(img image.Image) *image.Paletted {
return pm
}

func encode(w io.Writer, img image.Image, format imaging.Format, quality int) error {
func encode(w io.Writer, img image.Image, format imagefile.Format, quality int) error {
var err error
switch format {
case imaging.JPEG:
case imagefile.JPEG:
var rgba *image.RGBA
if nrgba, ok := img.(*image.NRGBA); ok {
if nrgba.Opaque() {
Expand All @@ -258,14 +264,16 @@ func encode(w io.Writer, img image.Image, format imaging.Format, quality int) er
err = jpeg.Encode(w, img, &jpeg.Options{Quality: quality})
}

case imaging.PNG:
case imagefile.PNG:
err = png.Encode(w, img)
case imaging.GIF:
case imagefile.GIF:
err = gif.Encode(w, img, &gif.Options{NumColors: 256})
case imaging.TIFF:
case imagefile.TIFF:
err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
case imaging.BMP:
case imagefile.BMP:
err = bmp.Encode(w, img)
case imagefile.WEBP:
err = webp.Encode(w, img, &webp.Options{Quality: float32(quality)})
default:
err = imaging.ErrUnsupportedFormat
}
Expand Down
2 changes: 1 addition & 1 deletion engine/backend/goimage_flat.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (e *GoImage) Flat(ctx context.Context, backgroundFile *imagefile.ImageFile,
}
}

if options.Format == imaging.GIF {
if options.Format == imagefile.GIF {
g, err := gif.DecodeAll(bytes.NewReader(backgroundFile.Source))
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions engine/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ var (
"image/gif",
"image/jpeg",
"image/png",
"image/webp",
}
)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
)

require (
github.com/chai2010/webp v1.1.1
github.com/google/uuid v1.3.0
github.com/prometheus/client_golang v1.14.0
golang.org/x/sync v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chai2010/webp v1.1.1 h1:jTRmEccAJ4MGrhFOrPMpNGIJ/eybIgwKpcACsrTEapk=
github.com/chai2010/webp v1.1.1/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
Expand Down
13 changes: 13 additions & 0 deletions image/formats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package image

type Format int

// Image file formats.
const (
JPEG Format = iota
PNG
GIF
TIFF
BMP
WEBP
)
15 changes: 8 additions & 7 deletions parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strconv"
"strings"

"github.com/go-spectest/imaging"
"github.com/pkg/errors"
"github.com/ulule/gostorages"

Expand All @@ -25,12 +24,14 @@ const (
defaultWidth = 0
)

var formats = map[string]imaging.Format{
"bmp": imaging.BMP,
"gif": imaging.GIF,
"jpeg": imaging.JPEG,
"jpg": imaging.JPEG,
"png": imaging.PNG,
var formats = map[string]image.Format{
"bmp": image.BMP,
"gif": image.GIF,
"jpeg": image.JPEG,
"jpg": image.JPEG,
"png": image.PNG,
"tiff": image.TIFF,
"webp": image.WEBP,
}

type Parameters struct {
Expand Down

0 comments on commit f684e81

Please sign in to comment.