Skip to content

Instantly share code, notes, and snippets.

@orian
Created Feb 8, 2016
Embed
What would you like to do?
Calculate size of resized image
package main
import (
"fmt"
"math"
)
func CalcNewSize(w, h uint, maxW, maxH int32) (uint, uint) {
haveF := float64(w) / float64(h)
wantF := float64(maxW) / float64(maxH)
if haveF < wantF {
hh := float64(maxH)
ww := math.Floor(haveF*hh + 0.5)
return uint(ww), uint(hh)
} else {
ww := float64(maxW)
hh := math.Floor(ww/haveF + 0.5)
return uint(ww), uint(hh)
}
}
func main() {
fmt.Println(CalcNewSize(100, 150, 50, 60))
fmt.Println("Hello, playground")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment