Skip to content

Commit 3280445

Browse files
committed
add webp support (decode only)
if any transformation is requested, webp images will be encoded and served as jpeg or png, defaulting to jpeg if no format is specified. Fixes #88
1 parent b9cc9df commit 3280445

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ imageproxy is a caching image proxy server written in go. It features:
44

55
- basic image adjustments like resizing, cropping, and rotation
66
- access control using host whitelists or request signing (HMAC-SHA256)
7-
- support for jpeg, png, and gif image formats (including animated gifs)
7+
- support for jpeg, png, webp (decode only), and gif image formats (including animated gifs)
88
- on-disk caching, respecting the cache headers of the original images
99
- easy deployment, since it's pure go
1010

@@ -197,6 +197,15 @@ However, you can use the `scaleUp` command-line flag to allow this to happen:
197197

198198
imageproxy -scaleUp true
199199

200+
### WebP support ###
201+
202+
Imageproxy can proxy remote webp images, but they will be served in either jpeg
203+
or png format (this is because the golang webp library only support decoding)
204+
if any transformation is requested. If no format is specified, imageproxy will
205+
use jpeg by default. If no transformation is requested (for example, if you
206+
are just using imageproxy as an SSL proxy) then the original webp image will be
207+
served as-is without any format conversion.
208+
200209
## Deploying ##
201210

202211
You can build and deploy imageproxy using any standard go toolchain, but here's

imageproxy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ func (t *TransformingTransport) RoundTrip(req *http.Request) (*http.Response, er
308308
fmt.Fprintf(buf, "%s %s\n", resp.Proto, resp.Status)
309309
resp.Header.WriteSubset(buf, map[string]bool{
310310
"Content-Length": true,
311-
"Content-Type": opt.Format != "",
311+
// exclude Content-Type header if the format may have changed during transformation
312+
"Content-Type": opt.Format != "" || resp.Header.Get("Content-Type") == "image/webp",
312313
})
313314
fmt.Fprintf(buf, "Content-Length: %d\n\n", len(img))
314315
buf.Write(img)

transform.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"image/png"
2424

2525
"github.com/disintegration/imaging"
26+
_ "golang.org/x/image/webp" // register webp format
2627
"willnorris.com/go/gifresize"
2728
)
2829

@@ -62,7 +63,7 @@ func Transform(img []byte, opt Options) ([]byte, error) {
6263
if err != nil {
6364
return nil, err
6465
}
65-
case "jpeg":
66+
case "jpeg", "webp": // default to encoding webp as jpeg
6667
quality := opt.Quality
6768
if quality == 0 {
6869
quality = defaultQuality

0 commit comments

Comments
 (0)