Skip to content

Instantly share code, notes, and snippets.

@orian
Created February 8, 2016 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orian/9ca8b767c814efd779e9 to your computer and use it in GitHub Desktop.
Save orian/9ca8b767c814efd779e9 to your computer and use it in GitHub Desktop.
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