golang 任意角度旋转图片

//任意角度 顺时针旋转图片
func RotateImage(src []byte,angle int)  ( []byte,error){
	//var img, _,err = image.Decode(bytes.NewReader(src))
	var img,err = GetImage(src)
	if err!=nil{
		return src,err
	}
	angle = angle % 360
	//弧度转换
	radian := float64(angle) * math.Pi / 180.0
	cos := math.Cos(float64(radian))
	sin := math.Sin(radian)
	//原图的宽高
	w := float64(img.Bounds().Dx())
	h := float64(img.Bounds().Dy())

	//新图高宽
	W := int((math.Max(math.Abs(float64(w*cos-h*sin)), math.Abs(w * cos + h * sin))))
	H := int( (math.Max(math.Abs(w * sin - h * cos), math.Abs(w * sin + h * cos))) )

	dst := image.NewNRGBA( image.Rect(0,0,W,H) )
	err = graphics.Rotate(dst,img,&graphics.RotateOptions{radian})
	if err!=nil{
		return src,err
	}
	// 以PNG格式保存文件
	var buf bytes.Buffer
	err = png.Encode(&buf, dst)
	if err != nil {
		return src,err
	}
	return buf.Bytes(),nil
}