Skip to content

Commit 0b5f0b0

Browse files
authored
Support webp (#145)
1 parent 7e49589 commit 0b5f0b0

File tree

7 files changed

+62
-36
lines changed

7 files changed

+62
-36
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Package imgconv provides basic image processing functions (resize, add watermark, format converter.).
1414

1515
All the image processing functions provided by the package accept any image type that implements `image.Image` interface
16-
as an input, include jpg(jpeg), png, gif, tif(tiff), bmp, webp(decode only) and pdf.
16+
as an input, include jpg(jpeg), png, gif, tif(tiff), bmp, webp and pdf.
1717

1818
## Installation
1919

@@ -30,6 +30,7 @@ This repo relies on the following third-party projects:
3030
* [disintegration/imaging](https://github.com/disintegration/imaging)
3131
* [pdfcpu/pdfcpu](https://github.com/pdfcpu/pdfcpu)
3232
* [hhrutter/tiff](https://github.com/hhrutter/tiff)
33+
* [HugoSmits86/nativewebp](github.com/HugoSmits86/nativewebp)
3334

3435
## Usage examples
3536

converter/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
)
1111

1212
require (
13+
github.com/HugoSmits86/nativewebp v1.1.0 // indirect
1314
github.com/hhrutter/lzw v1.0.0 // indirect
1415
github.com/hhrutter/tiff v1.0.1 // indirect
1516
github.com/mattn/go-runewidth v0.0.15 // indirect

converter/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/HugoSmits86/nativewebp v1.1.0 h1:4V8ftAa8nY7F4I2qof7A74qf2Fjnl3zSdllpnwpCG+E=
2+
github.com/HugoSmits86/nativewebp v1.1.0/go.mod h1:YNQuWenlVmSUUASVNhTDwf4d7FwYQGbGhklC8p72Vr8=
13
github.com/hhrutter/lzw v1.0.0 h1:laL89Llp86W3rRs83LvKbwYRx6INE8gDn0XNb1oXtm0=
24
github.com/hhrutter/lzw v1.0.0/go.mod h1:2HC6DJSn/n6iAZfgM3Pg+cP1KxeWc3ezG8bBqW5+WEo=
35
github.com/hhrutter/tiff v1.0.1 h1:MIus8caHU5U6823gx7C6jrfoEvfSTGtEFRiM8/LOzC0=

converter/main.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,27 @@ import (
2121
)
2222

2323
var (
24-
src = flag.String("src", "", "")
25-
dst = flag.String("dst", "output", "")
26-
test = flag.Bool("test", false, "")
27-
force = flag.Bool("force", false, "")
28-
pdf = flag.Bool("pdf", false, "")
29-
whiteBackground = flag.Bool("white-background", false, "")
30-
gray = flag.Bool("gray", false, "")
31-
quality = flag.Int("quality", 75, "")
32-
autoOrientation = flag.Bool("auto-orientation", false, "")
33-
watermark = flag.String("watermark", "", "")
34-
opacity = flag.Uint("opacity", 128, "")
35-
random = flag.Bool("random", false, "")
36-
offsetX = flag.Int("x", 0, "")
37-
offsetY = flag.Int("y", 0, "")
38-
width = flag.Int("width", 0, "")
39-
height = flag.Int("height", 0, "")
40-
percent = flag.Float64("percent", 0, "")
41-
worker = flag.Int("worker", 5, "")
42-
quiet = flag.Bool("q", false, "")
43-
debug = flag.Bool("debug", false, "")
24+
src = flag.String("src", "", "")
25+
dst = flag.String("dst", "output", "")
26+
test = flag.Bool("test", false, "")
27+
force = flag.Bool("force", false, "")
28+
pdf = flag.Bool("pdf", false, "")
29+
whiteBackground = flag.Bool("white-background", false, "")
30+
gray = flag.Bool("gray", false, "")
31+
quality = flag.Int("quality", 75, "")
32+
autoOrientation = flag.Bool("auto-orientation", false, "")
33+
useExtendedFormat = flag.Bool("use-extended-format", false, "")
34+
watermark = flag.String("watermark", "", "")
35+
opacity = flag.Uint("opacity", 128, "")
36+
random = flag.Bool("random", false, "")
37+
offsetX = flag.Int("x", 0, "")
38+
offsetY = flag.Int("y", 0, "")
39+
width = flag.Int("width", 0, "")
40+
height = flag.Int("height", 0, "")
41+
percent = flag.Float64("percent", 0, "")
42+
worker = flag.Int("worker", 5, "")
43+
quiet = flag.Bool("q", false, "")
44+
debug = flag.Bool("debug", false, "")
4445

4546
format imgconv.Format
4647
compression imgconv.TIFFCompression
@@ -60,7 +61,7 @@ func usage() {
6061
--pdf
6162
convert pdf source (default: false)
6263
--format
63-
output format (jpg, jpeg, png, gif, tif, tiff, bmp and pdf are supported, default: jpg)
64+
output format (jpg, jpeg, png, gif, tif, tiff, bmp, pdf and webp are supported, default: jpg)
6465
--white-background
6566
use white color for transparent background (default: false)
6667
--gray
@@ -71,6 +72,8 @@ func usage() {
7172
set tiff compression type (none, deflate, default: deflate)
7273
--auto-orientation
7374
auto orientation (default: false)
75+
--use-extended-format
76+
set webp to use extended format (default: false)
7477
--watermark
7578
watermark path
7679
--opacity
@@ -165,6 +168,9 @@ func main() {
165168
if format == imgconv.TIFF {
166169
opts = append(opts, imgconv.TIFFCompressionType(compression))
167170
}
171+
if format == imgconv.WEBP {
172+
opts = append(opts, imgconv.WEBPUseExtendedFormat(*useExtendedFormat))
173+
}
168174
if *whiteBackground {
169175
opts = append(opts, imgconv.BackgroundColor(color.White))
170176
}

format.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
"image/jpeg"
1111
"image/png"
1212
"io"
13+
"slices"
1314
"strings"
1415

16+
"github.com/HugoSmits86/nativewebp"
1517
"github.com/sunshineplan/pdf"
1618
"golang.org/x/image/bmp"
1719
"golang.org/x/image/tiff"
18-
_ "golang.org/x/image/webp" // decode webp format
1920
)
2021

2122
var (
@@ -34,6 +35,7 @@ const (
3435
TIFF
3536
BMP
3637
PDF
38+
WEBP
3739
)
3840

3941
var formatExts = [][]string{
@@ -43,6 +45,7 @@ var formatExts = [][]string{
4345
{"tif", "tiff"},
4446
{"bmp"},
4547
{"pdf"},
48+
{"webp"},
4649
}
4750

4851
func (f Format) String() (format string) {
@@ -55,14 +58,12 @@ func (f Format) String() (format string) {
5558
}
5659

5760
// FormatFromExtension parses image format from filename extension:
58-
// "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff"), "bmp" and "pdf" are supported.
61+
// "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff"), "bmp", "pdf" and "webp" are supported.
5962
func FormatFromExtension(ext string) (Format, error) {
6063
ext = strings.ToLower(ext)
6164
for index, exts := range formatExts {
62-
for _, i := range exts {
63-
if ext == i {
64-
return Format(index), nil
65-
}
65+
if slices.Contains(exts, ext) {
66+
return Format(index), nil
6667
}
6768
}
6869

@@ -137,13 +138,14 @@ type FormatOption struct {
137138
}
138139

139140
type encodeConfig struct {
140-
Quality int
141-
gifNumColors int
142-
gifQuantizer draw.Quantizer
143-
gifDrawer draw.Drawer
144-
pngCompressionLevel png.CompressionLevel
145-
tiffCompressionType TIFFCompression
146-
background color.Color
141+
Quality int
142+
gifNumColors int
143+
gifQuantizer draw.Quantizer
144+
gifDrawer draw.Drawer
145+
pngCompressionLevel png.CompressionLevel
146+
tiffCompressionType TIFFCompression
147+
webpUseExtendedFormat bool
148+
background color.Color
147149
}
148150

149151
var defaultEncodeConfig = encodeConfig{
@@ -207,14 +209,22 @@ func TIFFCompressionType(compressionType TIFFCompression) EncodeOption {
207209
}
208210
}
209211

212+
// WEBPUseExtendedFormat returns EncodeOption that determines whether to use extended format
213+
// of the WEBP-encoded image. Default is false.
214+
func WEBPUseExtendedFormat(b bool) EncodeOption {
215+
return func(c *encodeConfig) {
216+
c.webpUseExtendedFormat = b
217+
}
218+
}
219+
210220
// BackgroundColor returns an EncodeOption that sets the background color.
211221
func BackgroundColor(color color.Color) EncodeOption {
212222
return func(c *encodeConfig) {
213223
c.background = color
214224
}
215225
}
216226

217-
// Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF, BMP or PDF).
227+
// Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF, BMP, PDF or WEBP).
218228
func (f *FormatOption) Encode(w io.Writer, img image.Image) error {
219229
cfg := defaultEncodeConfig
220230
for _, option := range f.EncodeOption {
@@ -259,6 +269,9 @@ func (f *FormatOption) Encode(w io.Writer, img image.Image) error {
259269

260270
case PDF:
261271
return pdf.Encode(w, []image.Image{img}, &pdf.Options{Quality: cfg.Quality})
272+
273+
case WEBP:
274+
return nativewebp.Encode(w, img, &nativewebp.Options{UseExtendedFormat: cfg.webpUseExtendedFormat})
262275
}
263276

264277
return image.ErrFormat

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/sunshineplan/imgconv
33
go 1.24
44

55
require (
6+
github.com/HugoSmits86/nativewebp v1.1.0
67
github.com/sunshineplan/pdf v1.0.7
78
golang.org/x/image v0.25.0
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/HugoSmits86/nativewebp v1.1.0 h1:4V8ftAa8nY7F4I2qof7A74qf2Fjnl3zSdllpnwpCG+E=
2+
github.com/HugoSmits86/nativewebp v1.1.0/go.mod h1:YNQuWenlVmSUUASVNhTDwf4d7FwYQGbGhklC8p72Vr8=
13
github.com/hhrutter/lzw v1.0.0 h1:laL89Llp86W3rRs83LvKbwYRx6INE8gDn0XNb1oXtm0=
24
github.com/hhrutter/lzw v1.0.0/go.mod h1:2HC6DJSn/n6iAZfgM3Pg+cP1KxeWc3ezG8bBqW5+WEo=
35
github.com/hhrutter/tiff v1.0.1 h1:MIus8caHU5U6823gx7C6jrfoEvfSTGtEFRiM8/LOzC0=

0 commit comments

Comments
 (0)