mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-16 21:56:43 -05:00
Add quality option
- Added 'q' option to specify quality (used for JPEGs) - Updated tests - Update documentation
This commit is contained in:
parent
0980ea64a0
commit
e7b0a5b88c
4 changed files with 34 additions and 8 deletions
|
@ -66,6 +66,11 @@ The `r{degrees}` option will rotate the image the specified number of degrees,
|
|||
counter-clockwise. Valid degrees values are `90`, `180`, and `270`. Images
|
||||
are rotated **after** being resized.
|
||||
|
||||
#### Quality ####
|
||||
|
||||
The `q{percentage}` option can be used to specify the output quality (JPEG
|
||||
only).
|
||||
|
||||
#### Flip ####
|
||||
|
||||
The `fv` option will flip the image vertically. The `fh` option will flip the
|
||||
|
@ -99,6 +104,7 @@ x100 | 100px tall, proportional width | <a href="https://willnorris
|
|||
150,fit | scale to fit 150px square, no cropping | <a href="https://willnorris.com/api/imageproxy/150,fit/https://willnorris.com/2013/12/small-things.jpg"><img src="https://willnorris.com/api/imageproxy/150,fit/https://willnorris.com/2013/12/small-things.jpg" alt="150,fit"></a>
|
||||
100,r90 | 100px square, rotated 90 degrees | <a href="https://willnorris.com/api/imageproxy/100,r90/https://willnorris.com/2013/12/small-things.jpg"><img src="https://willnorris.com/api/imageproxy/100,r90/https://willnorris.com/2013/12/small-things.jpg" alt="100,r90"></a>
|
||||
100,fv,fh | 100px square, flipped horizontal and vertical | <a href="https://willnorris.com/api/imageproxy/100,fv,fh/https://willnorris.com/2013/12/small-things.jpg"><img src="https://willnorris.com/api/imageproxy/100,fv,fh/https://willnorris.com/2013/12/small-things.jpg" alt="100,fv,fh"></a>
|
||||
200x,q60 | 200px wide, proportional height, 60% quality | <a href="https://willnorris.com/api/imageproxy/200x,q60/https://willnorris.com/2013/12/small-things.jpg"><img src="https://willnorris.com/api/imageproxy/200x,q60/https://willnorris.com/2013/12/small-things.jpg" alt="200x,q60"></a>
|
||||
|
||||
|
||||
## Getting Started ##
|
||||
|
|
14
data.go
14
data.go
|
@ -28,6 +28,7 @@ const (
|
|||
optFlipVertical = "fv"
|
||||
optFlipHorizontal = "fh"
|
||||
optRotatePrefix = "r"
|
||||
optQualityPrefix = "q"
|
||||
optSizeDelimiter = "x"
|
||||
)
|
||||
|
||||
|
@ -57,6 +58,9 @@ type Options struct {
|
|||
|
||||
FlipVertical bool
|
||||
FlipHorizontal bool
|
||||
|
||||
// Quality of output image
|
||||
Quality int
|
||||
}
|
||||
|
||||
var emptyOptions = Options{}
|
||||
|
@ -76,6 +80,7 @@ func (o Options) String() string {
|
|||
if o.FlipHorizontal {
|
||||
fmt.Fprintf(buf, ",%s", optFlipHorizontal)
|
||||
}
|
||||
fmt.Fprintf(buf, ",%s%d", string(optQualityPrefix), o.Quality)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
|
@ -119,6 +124,11 @@ func (o Options) String() string {
|
|||
// The "fv" option will flip the image vertically. The "fh" option will flip
|
||||
// the image horizontally. Images are flipped after being rotated.
|
||||
//
|
||||
// Quality
|
||||
//
|
||||
// The "q{qualityPercentage}" option can be used to specify the quality of the
|
||||
// output file (JPEG only)
|
||||
//
|
||||
// Examples
|
||||
//
|
||||
// 0x0 - no resizing
|
||||
|
@ -130,6 +140,7 @@ func (o Options) String() string {
|
|||
// 150,fit - scale to fit 150 pixels square, no cropping
|
||||
// 100,r90 - 100 pixels square, rotated 90 degrees
|
||||
// 100,fv,fh - 100 pixels square, flipped horizontal and vertical
|
||||
// 200x,q80 - 200 pixels wide, proportional height, 80% quality
|
||||
func ParseOptions(str string) Options {
|
||||
options := Options{}
|
||||
|
||||
|
@ -146,6 +157,9 @@ func ParseOptions(str string) Options {
|
|||
case strings.HasPrefix(opt, optRotatePrefix):
|
||||
value := strings.TrimPrefix(opt, optRotatePrefix)
|
||||
options.Rotate, _ = strconv.Atoi(value)
|
||||
case strings.HasPrefix(opt, optQualityPrefix):
|
||||
value := strings.TrimPrefix(opt, optQualityPrefix)
|
||||
options.Quality, _ = strconv.Atoi(value)
|
||||
case strings.Contains(opt, optSizeDelimiter):
|
||||
size := strings.SplitN(opt, optSizeDelimiter, 2)
|
||||
if w := size[0]; w != "" {
|
||||
|
|
14
data_test.go
14
data_test.go
|
@ -26,15 +26,15 @@ func TestOptions_String(t *testing.T) {
|
|||
}{
|
||||
{
|
||||
emptyOptions,
|
||||
"0x0",
|
||||
"0x0,q0",
|
||||
},
|
||||
{
|
||||
Options{1, 2, true, 90, true, true},
|
||||
"1x2,fit,r90,fv,fh",
|
||||
Options{1, 2, true, 90, true, true, 80},
|
||||
"1x2,fit,r90,fv,fh,q80",
|
||||
},
|
||||
{
|
||||
Options{0.15, 1.3, false, 45, false, false},
|
||||
"0.15x1.3,r45",
|
||||
Options{0.15, 1.3, false, 45, false, false, 95},
|
||||
"0.15x1.3,r45,q95",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -82,8 +82,8 @@ func TestParseOptions(t *testing.T) {
|
|||
{"FOO,1,BAR,r90,BAZ", Options{Width: 1, Height: 1, Rotate: 90}},
|
||||
|
||||
// all flags, in different orders
|
||||
{"1x2,fit,r90,fv,fh", Options{1, 2, true, 90, true, true}},
|
||||
{"r90,fh,1x2,fv,fit", Options{1, 2, true, 90, true, true}},
|
||||
{"q70,1x2,fit,r90,fv,fh", Options{1, 2, true, 90, true, true, 70}},
|
||||
{"r90,fh,q90,1x2,fv,fit", Options{1, 2, true, 90, true, true, 90}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
@ -53,7 +53,13 @@ func Transform(img []byte, opt Options) ([]byte, error) {
|
|||
case "gif":
|
||||
gif.Encode(buf, m, nil)
|
||||
case "jpeg":
|
||||
jpeg.Encode(buf, m, &jpeg.Options{Quality: jpegQuality})
|
||||
quality := opt.Quality
|
||||
|
||||
if quality == 0 {
|
||||
quality = jpegQuality
|
||||
}
|
||||
|
||||
jpeg.Encode(buf, m, &jpeg.Options{Quality: quality})
|
||||
case "png":
|
||||
png.Encode(buf, m)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue