mirror of
https://github.com/willnorris/imageproxy.git
synced 2025-03-11 02:19:14 -05:00
Skip resizing if requested size bigger then original
This commit is contained in:
parent
431898ec9a
commit
8efff4b6a9
2 changed files with 21 additions and 15 deletions
10
transform.go
10
transform.go
|
@ -79,7 +79,7 @@ func Transform(img []byte, opt Options) ([]byte, error) {
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resizeParams(m image.Image, opt *Options) (w, h int) {
|
func resizeParams(m image.Image, opt *Options) (w, h int, resize bool) {
|
||||||
// convert percentage width and height values to absolute values
|
// convert percentage width and height values to absolute values
|
||||||
imgW := m.Bounds().Max.X - m.Bounds().Min.X
|
imgW := m.Bounds().Max.X - m.Bounds().Min.X
|
||||||
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
|
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
|
||||||
|
@ -107,14 +107,18 @@ func resizeParams(m image.Image, opt *Options) (w, h int) {
|
||||||
h = imgH
|
h = imgH
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
// if requested width and height match the original, skip resizing
|
||||||
|
if (w == imgW || w == 0) && (h == imgH || h == 0) {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
return w, h, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// transformImage modifies the image m based on the transformations specified
|
// transformImage modifies the image m based on the transformations specified
|
||||||
// in opt.
|
// in opt.
|
||||||
func transformImage(m image.Image, opt Options) image.Image {
|
func transformImage(m image.Image, opt Options) image.Image {
|
||||||
// resize if needed
|
// resize if needed
|
||||||
if w, h := resizeParams(m, &opt); w != 0 || h != 0 {
|
if w, h, resize := resizeParams(m, &opt); resize {
|
||||||
if opt.Fit {
|
if opt.Fit {
|
||||||
m = imaging.Fit(m, w, h, resampleFilter)
|
m = imaging.Fit(m, w, h, resampleFilter)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -40,20 +40,22 @@ func newImage(w, h int, pixels ...color.NRGBA) image.Image {
|
||||||
func TestResizeParams(t *testing.T) {
|
func TestResizeParams(t *testing.T) {
|
||||||
src := image.NewNRGBA(image.Rect(0, 0, 64, 128))
|
src := image.NewNRGBA(image.Rect(0, 0, 64, 128))
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
opt Options
|
opt Options
|
||||||
w,h int
|
w, h int
|
||||||
|
resize bool
|
||||||
}{
|
}{
|
||||||
{Options{Width:0.5}, 32, 0},
|
{Options{Width: 0.5}, 32, 0, true},
|
||||||
{Options{Height:0.5}, 0, 64},
|
{Options{Height: 0.5}, 0, 64, true},
|
||||||
{Options{Width:0.5, Height:0.5}, 32, 64},
|
{Options{Width: 0.5, Height: 0.5}, 32, 64, true},
|
||||||
{Options{Width:100, Height: 200}, 64, 128},
|
{Options{Width: 100, Height: 200}, 0, 0, false},
|
||||||
{Options{Width:64}, 64, 0},
|
{Options{Width: 100, Height: 200, ScaleUp: true}, 100, 200, true},
|
||||||
{Options{Height:128}, 0, 128},
|
{Options{Width: 64}, 0, 0, false},
|
||||||
|
{Options{Height: 128}, 0, 0, false},
|
||||||
}
|
}
|
||||||
for ti,tt := range tests {
|
for ti, tt := range tests {
|
||||||
w,h := resizeParams(src, &tt.opt)
|
w, h, resize := resizeParams(src, &tt.opt)
|
||||||
if w != tt.w || h!= tt.h {
|
if w != tt.w || h != tt.h || resize != tt.resize {
|
||||||
t.Errorf("test %d problem, want: %d,%d got: %d,%d", ti, tt.w, tt.h, w, h)
|
t.Errorf("test %d problem, want: %d,%d,%t got: %d,%d,%t", ti, tt.w, tt.h, tt.resize, w, h, resize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue