mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-30 22:34:18 -05:00
add evaluateFloat helper function
This commit is contained in:
parent
a33df59013
commit
81cb74ef7e
1 changed files with 15 additions and 14 deletions
29
transform.go
29
transform.go
|
@ -88,26 +88,27 @@ func Transform(img []byte, opt Options) ([]byte, error) {
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// evaluateFloat interprets the option value f. If f is between 0 and 1, it is
|
||||||
|
// interpreted as a percentage of max, otherwise it is treated as an absolute
|
||||||
|
// value. If f is less than 0, 0 is returned.
|
||||||
|
func evaluateFloat(f float64, max int) int {
|
||||||
|
if 0 < f && f < 1 {
|
||||||
|
return int(float64(max) * f)
|
||||||
|
}
|
||||||
|
if f < 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return int(f)
|
||||||
|
}
|
||||||
|
|
||||||
// resizeParams determines if the image needs to be resized, and if so, the
|
// resizeParams determines if the image needs to be resized, and if so, the
|
||||||
// dimensions to resize to.
|
// dimensions to resize to.
|
||||||
func resizeParams(m image.Image, opt Options) (w, h int, resize bool) {
|
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
|
||||||
if 0 < opt.Width && opt.Width < 1 {
|
w = evaluateFloat(opt.Width, imgW)
|
||||||
w = int(float64(imgW) * opt.Width)
|
h = evaluateFloat(opt.Height, imgH)
|
||||||
} else if opt.Width < 0 {
|
|
||||||
w = 0
|
|
||||||
} else {
|
|
||||||
w = int(opt.Width)
|
|
||||||
}
|
|
||||||
if 0 < opt.Height && opt.Height < 1 {
|
|
||||||
h = int(float64(imgH) * opt.Height)
|
|
||||||
} else if opt.Height < 0 {
|
|
||||||
h = 0
|
|
||||||
} else {
|
|
||||||
h = int(opt.Height)
|
|
||||||
}
|
|
||||||
|
|
||||||
// never resize larger than the original image unless specifically allowed
|
// never resize larger than the original image unless specifically allowed
|
||||||
if !opt.ScaleUp {
|
if !opt.ScaleUp {
|
||||||
|
|
Loading…
Reference in a new issue