Пример #1
0
func fitCropScale(i image.Image, r image.Rectangle) image.Image {
	wantRatio := float64(r.Bounds().Dx()) / float64(r.Bounds().Dy())
	haveRatio := float64(i.Bounds().Dx()) / float64(i.Bounds().Dy())

	sliceRect := image.Rectangle{}

	if haveRatio > wantRatio {
		wantwidth := wantRatio * float64(i.Bounds().Dy())
		sliceRect = image.Rect(i.Bounds().Dx()/2-int(wantwidth/2), 0, i.Bounds().Dx()/2+int(wantwidth/2), i.Bounds().Dy())
	} else {
		wantheight := float64(i.Bounds().Dx()) / wantRatio
		sliceRect = image.Rect(0, i.Bounds().Dy()/2-int(wantheight/2), i.Bounds().Dx(), i.Bounds().Dy()/2+int(wantheight/2))
	}

	return imaging.Resize(imaging.Crop(i, sliceRect), r.Dx(), r.Dy(), imaging.Lanczos)
}