mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-16 21:56:43 -05:00
refactor cropParams to return image.Rectangle
This commit is contained in:
parent
9fcdcadfa4
commit
8eee5513ab
2 changed files with 29 additions and 33 deletions
36
transform.go
36
transform.go
|
@ -131,8 +131,8 @@ func evaluateFloat(f float64, max int) int {
|
|||
// dimensions to resize to.
|
||||
func resizeParams(m image.Image, opt Options) (w, h int, resize bool) {
|
||||
// convert percentage width and height values to absolute values
|
||||
imgW := m.Bounds().Max.X - m.Bounds().Min.X
|
||||
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
|
||||
imgW := m.Bounds().Dx()
|
||||
imgH := m.Bounds().Dy()
|
||||
w = evaluateFloat(opt.Width, imgW)
|
||||
h = evaluateFloat(opt.Height, imgH)
|
||||
|
||||
|
@ -155,23 +155,23 @@ func resizeParams(m image.Image, opt Options) (w, h int, resize bool) {
|
|||
}
|
||||
|
||||
// cropParams calculates crop rectangle parameters to keep it in image bounds
|
||||
func cropParams(m image.Image, opt Options) (x0, y0, x1, y1 int, crop bool) {
|
||||
func cropParams(m image.Image, opt Options) image.Rectangle {
|
||||
if opt.CropX == 0 && opt.CropY == 0 && opt.CropWidth == 0 && opt.CropHeight == 0 {
|
||||
return 0, 0, 0, 0, false
|
||||
return m.Bounds()
|
||||
}
|
||||
|
||||
// width and height of image
|
||||
imgW := m.Bounds().Max.X - m.Bounds().Min.X
|
||||
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
|
||||
imgW := m.Bounds().Dx()
|
||||
imgH := m.Bounds().Dy()
|
||||
|
||||
// top left coordinate of crop
|
||||
x0 = evaluateFloat(math.Abs(opt.CropX), imgW)
|
||||
x0 := evaluateFloat(math.Abs(opt.CropX), imgW)
|
||||
if opt.CropX < 0 {
|
||||
x0 = imgW - x0
|
||||
x0 = imgW - x0 // measure from right
|
||||
}
|
||||
y0 = evaluateFloat(math.Abs(opt.CropY), imgH)
|
||||
y0 := evaluateFloat(math.Abs(opt.CropY), imgH)
|
||||
if opt.CropY < 0 {
|
||||
y0 = imgH - y0
|
||||
y0 = imgH - y0 // measure from bottom
|
||||
}
|
||||
|
||||
// width and height of crop
|
||||
|
@ -184,21 +184,17 @@ func cropParams(m image.Image, opt Options) (x0, y0, x1, y1 int, crop bool) {
|
|||
h = imgH
|
||||
}
|
||||
|
||||
if x0 == 0 && y0 == 0 && w == imgW && h == imgH {
|
||||
return 0, 0, 0, 0, false
|
||||
}
|
||||
|
||||
// bottom right coordinate of crop
|
||||
x1 = x0 + w
|
||||
x1 := x0 + w
|
||||
if x1 > imgW {
|
||||
x1 = imgW
|
||||
}
|
||||
y1 = y0 + h
|
||||
y1 := y0 + h
|
||||
if y1 > imgH {
|
||||
y1 = imgH
|
||||
}
|
||||
|
||||
return x0, y0, x1, y1, true
|
||||
return image.Rect(x0, y0, x1, y1)
|
||||
}
|
||||
|
||||
// read EXIF orientation tag from r and adjust opt to orient image correctly.
|
||||
|
@ -256,11 +252,11 @@ func exifOrientation(r io.Reader) (opt Options) {
|
|||
// in opt.
|
||||
func transformImage(m image.Image, opt Options) image.Image {
|
||||
// crop if needed
|
||||
if x0, y0, x1, y1, crop := cropParams(m, opt); crop {
|
||||
m = imaging.Crop(m, image.Rect(x0, y0, x1, y1))
|
||||
if r := cropParams(m, opt); !m.Bounds().Eq(r) {
|
||||
m = imaging.Crop(m, r)
|
||||
}
|
||||
// resize if needed
|
||||
if w, h, resize := resizeParams(m, opt); resize {
|
||||
if w, h, ok := resizeParams(m, opt); ok {
|
||||
if opt.Fit {
|
||||
m = imaging.Fit(m, w, h, resampleFilter)
|
||||
} else {
|
||||
|
|
|
@ -80,22 +80,22 @@ func TestCropParams(t *testing.T) {
|
|||
tests := []struct {
|
||||
opt Options
|
||||
x0, y0, x1, y1 int
|
||||
crop bool
|
||||
}{
|
||||
{Options{CropWidth: 10, CropHeight: 0}, 0, 0, 10, 128, true},
|
||||
{Options{CropWidth: 0, CropHeight: 10}, 0, 0, 64, 10, true},
|
||||
{Options{CropWidth: -1, CropHeight: -1}, 0, 0, 0, 0, false},
|
||||
{Options{CropWidth: 50, CropHeight: 100}, 0, 0, 50, 100, true},
|
||||
{Options{CropWidth: 100, CropHeight: 100}, 0, 0, 64, 100, true},
|
||||
{Options{CropX: 50, CropY: 100}, 50, 100, 64, 128, true},
|
||||
{Options{CropX: 50, CropY: 100, CropWidth: 100, CropHeight: 150}, 50, 100, 64, 128, true},
|
||||
{Options{CropX: -50, CropY: -50}, 14, 78, 64, 128, true},
|
||||
{Options{CropY: 0.5, CropWidth: 0.5}, 0, 64, 32, 128, true},
|
||||
{Options{CropWidth: 10, CropHeight: 0}, 0, 0, 10, 128},
|
||||
{Options{CropWidth: 0, CropHeight: 10}, 0, 0, 64, 10},
|
||||
{Options{CropWidth: -1, CropHeight: -1}, 0, 0, 64, 128},
|
||||
{Options{CropWidth: 50, CropHeight: 100}, 0, 0, 50, 100},
|
||||
{Options{CropWidth: 100, CropHeight: 100}, 0, 0, 64, 100},
|
||||
{Options{CropX: 50, CropY: 100}, 50, 100, 64, 128},
|
||||
{Options{CropX: 50, CropY: 100, CropWidth: 100, CropHeight: 150}, 50, 100, 64, 128},
|
||||
{Options{CropX: -50, CropY: -50}, 14, 78, 64, 128},
|
||||
{Options{CropY: 0.5, CropWidth: 0.5}, 0, 64, 32, 128},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
x0, y0, x1, y1, crop := cropParams(src, tt.opt)
|
||||
if x0 != tt.x0 || y0 != tt.y0 || x1 != tt.x1 || y1 != tt.y1 || crop != tt.crop {
|
||||
t.Errorf("cropParams(%v) returned (%d,%d,%d,%d,%t), want (%d,%d,%d,%d,%t)", tt.opt, x0, y0, x1, y1, crop, tt.x0, tt.y0, tt.x1, tt.y1, tt.crop)
|
||||
want := image.Rect(tt.x0, tt.y0, tt.x1, tt.y1)
|
||||
got := cropParams(src, tt.opt)
|
||||
if !got.Eq(want) {
|
||||
t.Errorf("cropParams(%v) returned %v, want %v", tt.opt, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue