mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-05-27 04:45:12 +02:00
allow percentage sizes
change height and width options to be floats, and interpret values between 0 and 1 to be percentages. For example, an options strings of "0.5x" specifies 50% width and proportional height.
This commit is contained in:
parent
036d0c51c4
commit
cd1112f7ac
4 changed files with 28 additions and 12 deletions
10
data/data.go
10
data/data.go
|
|
@ -12,8 +12,8 @@ import (
|
||||||
// Options specifies transformations that can be performed on a
|
// Options specifies transformations that can be performed on a
|
||||||
// requested image.
|
// requested image.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
Width int // requested width, in pixels
|
Width float64 // requested width, in pixels
|
||||||
Height int // requested height, in pixels
|
Height float64 // requested height, in pixels
|
||||||
|
|
||||||
// If true, resize the image to fit in the specified dimensions. Image
|
// If true, resize the image to fit in the specified dimensions. Image
|
||||||
// will not be cropped, and aspect ratio will be maintained.
|
// will not be cropped, and aspect ratio will be maintained.
|
||||||
|
|
@ -21,7 +21,7 @@ type Options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o Options) String() string {
|
func (o Options) String() string {
|
||||||
return fmt.Sprintf("%dx%d", o.Width, o.Height)
|
return fmt.Sprintf("%vx%v", o.Width, o.Height)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseOptions(str string) *Options {
|
func ParseOptions(str string) *Options {
|
||||||
|
|
@ -40,10 +40,10 @@ func ParseOptions(str string) *Options {
|
||||||
}
|
}
|
||||||
|
|
||||||
if w != "" {
|
if w != "" {
|
||||||
o.Width, _ = strconv.Atoi(w)
|
o.Width, _ = strconv.ParseFloat(w, 64)
|
||||||
}
|
}
|
||||||
if h != "" {
|
if h != "" {
|
||||||
o.Height, _ = strconv.Atoi(h)
|
o.Height, _ = strconv.ParseFloat(h, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, part := range parts[1:] {
|
for _, part := range parts[1:] {
|
||||||
|
|
|
||||||
|
|
@ -91,11 +91,11 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.MaxWidth > 0 && req.Options.Width > p.MaxWidth {
|
if p.MaxWidth > 0 && int(req.Options.Width) > p.MaxWidth {
|
||||||
req.Options.Width = p.MaxWidth
|
req.Options.Width = float64(p.MaxWidth)
|
||||||
}
|
}
|
||||||
if p.MaxHeight > 0 && req.Options.Height > p.MaxHeight {
|
if p.MaxHeight > 0 && int(req.Options.Height) > p.MaxHeight {
|
||||||
req.Options.Height = p.MaxHeight
|
req.Options.Height = float64(p.MaxHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
u := req.URL.String()
|
u := req.URL.String()
|
||||||
|
|
|
||||||
|
|
@ -88,6 +88,10 @@ func TestNewRequest(t *testing.T) {
|
||||||
"http://localhost/1x2,fit/http://example.com/",
|
"http://localhost/1x2,fit/http://example.com/",
|
||||||
"http://example.com/", &data.Options{1, 2, true}, false,
|
"http://example.com/", &data.Options{1, 2, true}, false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"http://localhost/0.1x0.2,fit/http://example.com/",
|
||||||
|
"http://example.com/", &data.Options{0.1, 0.2, true}, false,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
|
|
|
||||||
|
|
@ -35,14 +35,26 @@ func Transform(img data.Image, opt *data.Options) (*data.Image, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var h, w int
|
||||||
|
if opt.Width > 0 && opt.Width < 1 {
|
||||||
|
w = int(float64(m.Bounds().Max.X-m.Bounds().Min.X) * opt.Width)
|
||||||
|
} else {
|
||||||
|
w = int(opt.Width)
|
||||||
|
}
|
||||||
|
if opt.Height > 0 && opt.Height < 1 {
|
||||||
|
h = int(float64(m.Bounds().Max.Y-m.Bounds().Min.Y) * opt.Height)
|
||||||
|
} else {
|
||||||
|
h = int(opt.Height)
|
||||||
|
}
|
||||||
|
|
||||||
// resize
|
// resize
|
||||||
if opt.Fit {
|
if opt.Fit {
|
||||||
m = imaging.Fit(m, opt.Width, opt.Height, imaging.Lanczos)
|
m = imaging.Fit(m, w, h, imaging.Lanczos)
|
||||||
} else {
|
} else {
|
||||||
if opt.Width == 0 || opt.Height == 0 {
|
if opt.Width == 0 || opt.Height == 0 {
|
||||||
m = imaging.Resize(m, opt.Width, opt.Height, imaging.Lanczos)
|
m = imaging.Resize(m, w, h, imaging.Lanczos)
|
||||||
} else {
|
} else {
|
||||||
m = imaging.Thumbnail(m, opt.Width, opt.Height, imaging.Lanczos)
|
m = imaging.Thumbnail(m, w, h, imaging.Lanczos)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue