From 817908938e98f557e99e7d7f0d4cfaf2eb9cf810 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 31 Aug 2017 17:03:17 -0700 Subject: [PATCH] allow forcing encoding as tiff --- README.md | 9 +++++++-- data.go | 9 +++++---- transform.go | 15 +++++++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1f1b676..7d2d6c1 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ imageproxy is a caching image proxy server written in go. It features: - basic image adjustments like resizing, cropping, and rotation - access control using host whitelists or request signing (HMAC-SHA256) - - support for jpeg, png, webp & tiff (decode only), and gif image formats (including animated gifs) + - support for jpeg, png, webp (decode only), tiff, and gif image formats (including animated gifs) - on-disk caching, respecting the cache headers of the original images - easy deployment, since it's pure go @@ -192,7 +192,7 @@ However, you can use the `scaleUp` command-line flag to allow this to happen: imageproxy -scaleUp true -### WebP support ### +### WebP and TIFF support ### Imageproxy can proxy remote webp images, but they will be served in either jpeg or png format (this is because the golang webp library only supports webp @@ -201,6 +201,11 @@ imageproxy will use jpeg by default. If no transformation is requested (for example, if you are just using imageproxy as an SSL proxy) then the original webp image will be served as-is without any format conversion. +Because so few browsers support tiff images, they will be converted to jpeg by +default if any transformation is requested. To force encoding as tiff, pass the +"tiff" option. Like webp, tiff images will be served as-is without any format +conversion if no transformation is requested. + Run `imageproxy -help` for a complete list of flags the command accepts. If you want to use a different caching implementation, it's probably easiest to diff --git a/data.go b/data.go index 652072e..f1f9299 100644 --- a/data.go +++ b/data.go @@ -29,6 +29,7 @@ const ( optFlipHorizontal = "fh" optFormatJPEG = "jpeg" optFormatPNG = "png" + optFormatTIFF = "tiff" optRotatePrefix = "r" optQualityPrefix = "q" optSignaturePrefix = "s" @@ -77,7 +78,7 @@ type Options struct { // will always be overwritten by the value of Proxy.ScaleUp. ScaleUp bool - // Desired image format. Valid values are "jpeg", "png". + // Desired image format. Valid values are "jpeg", "png", "tiff". Format string // Crop rectangle params @@ -202,8 +203,8 @@ func (o Options) transform() bool { // // Format // -// The "jpeg" and "png" options can be used to specify the desired image format -// of the proxied image. +// The "jpeg", "png", and "tiff" options can be used to specify the desired +// image format of the proxied image. // // Signature // @@ -243,7 +244,7 @@ func ParseOptions(str string) Options { options.FlipHorizontal = true case opt == optScaleUp: // this option is intentionally not documented above options.ScaleUp = true - case opt == optFormatJPEG, opt == optFormatPNG: + case opt == optFormatJPEG, opt == optFormatPNG, opt == optFormatTIFF: options.Format = opt case strings.HasPrefix(opt, optRotatePrefix): value := strings.TrimPrefix(opt, optRotatePrefix) diff --git a/transform.go b/transform.go index 1e00063..cfa00bf 100644 --- a/transform.go +++ b/transform.go @@ -24,8 +24,8 @@ import ( "math" "github.com/disintegration/imaging" + "golang.org/x/image/tiff" // register tiff format _ "golang.org/x/image/webp" // register webp format - _ "golang.org/x/image/tiff" // register tiff format "willnorris.com/go/gifresize" ) @@ -50,6 +50,11 @@ func Transform(img []byte, opt Options) ([]byte, error) { return nil, err } + // encode webp and tiff as jpeg by default + if format == "tiff" || format == "webp" { + format = "jpeg" + } + if opt.Format != "" { format = opt.Format } @@ -65,7 +70,7 @@ func Transform(img []byte, opt Options) ([]byte, error) { if err != nil { return nil, err } - case "jpeg", "webp", "tiff": // default to encoding webp & tiff as jpeg + case "jpeg": quality := opt.Quality if quality == 0 { quality = defaultQuality @@ -82,6 +87,12 @@ func Transform(img []byte, opt Options) ([]byte, error) { if err != nil { return nil, err } + case "tiff": + m = transformImage(m, opt) + err = tiff.Encode(buf, m, &tiff.Options{tiff.Deflate, true}) + if err != nil { + return nil, err + } default: return nil, fmt.Errorf("unsupported format: %v", format) }