Skip to content
Merged
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
7 changes: 7 additions & 0 deletions cmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
var jpgExtensions = []string{".jpg", ".jpeg"}
var pngExtensions = []string{".png"}
var bmpExtensions = []string{".bmp"}
var webpExtensions = []string{".webp"}

var (
// ErrWrongSize is thrown when the provided size string does not match the expected form.
Expand Down Expand Up @@ -51,6 +52,12 @@ func resolveEncoder(outputfile string, defaultEncoding imgio.Encoder) imgio.Enco
}
}

for _, ext := range webpExtensions {
if strings.HasSuffix(lower, ext) {
return imgio.WEBPEncoder(nil)
}
}

return defaultEncoding
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/anthonynsimon/bild
go 1.26

require (
github.com/HugoSmits86/nativewebp v1.3.0
github.com/spf13/cobra v1.10.2
golang.org/x/image v0.38.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/HugoSmits86/nativewebp v1.3.0 h1:n1egtEzSV4KwFtealr7dzdYq1wI/uj/bOQ/QcTcIyVE=
github.com/HugoSmits86/nativewebp v1.3.0/go.mod h1:YNQuWenlVmSUUASVNhTDwf4d7FwYQGbGhklC8p72Vr8=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down
8 changes: 8 additions & 0 deletions imgio/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"

"github.com/HugoSmits86/nativewebp"
"golang.org/x/image/bmp"
)

Expand Down Expand Up @@ -57,6 +58,13 @@ func BMPEncoder() Encoder {
}
}

// WEBPEncoder returns an encoder to WEBP
func WEBPEncoder(o *nativewebp.Options) Encoder {
return func(w io.Writer, img image.Image) error {
return nativewebp.Encode(w, img, o)
}
}

// Save creates a file and writes to it an image using the provided encoder.
//
// Usage example:
Expand Down
13 changes: 13 additions & 0 deletions imgio/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ func TestEncode(t *testing.T) {
},
},
},
{
format: "webp",
encoder: WEBPEncoder(nil),
value: &image.RGBA{
Rect: image.Rect(0, 0, 3, 3),
Stride: 3 * 4,
Pix: []uint8{
0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0x80, 0x00, 0x00, 0xFF,
0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF,
},
},
},
}

for _, c := range cases {
Expand Down
Loading