From 4e2e6f312e84f4066a61378971c48bec5828f9b9 Mon Sep 17 00:00:00 2001 From: ajin Date: Sat, 5 Nov 2022 21:26:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E5=9C=86=E8=A7=92?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/main.go | 5 ++++- image.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/examples/main.go b/examples/main.go index efb7bcf..f559d02 100644 --- a/examples/main.go +++ b/examples/main.go @@ -18,6 +18,8 @@ func main() { cutImage := loadImage.Cut(800, 200, 200, 200) //圆形剪切 circleImage := loadImage.Circle(850, 300, 100) + //圆角 + borderImage := cutImage.BorderRadius(20, 20, 20, 20) //椭圆剪切 ellipseImage := loadImage.Ellipse(850, 300, 75, 100) //色调、饱和度、亮度、不透明度调整 @@ -29,7 +31,8 @@ func main() { circleImage.SetArea(0, 400, circleImage.Width(), circleImage.Height()) ellipseImage.SetArea(0, 600, ellipseImage.Width(), ellipseImage.Height()) colorImage.SetArea(0, 800, colorImage.Width(), colorImage.Height()) - loadImage.Fill(resizeImage, cutImage, circleImage, ellipseImage, colorImage) + borderImage.SetArea(300, 800, colorImage.Width(), colorImage.Height()) + loadImage.Fill(resizeImage, cutImage, circleImage, ellipseImage, colorImage, borderImage) //文字自动分行 fillText := imagedraw.NewText("夜雨寄北(李商隐)君问归期未有期,巴山夜雨涨秋池。何当共剪西窗烛,却话巴山夜雨时。") diff --git a/image.go b/image.go index 1edf194..879088e 100644 --- a/image.go +++ b/image.go @@ -609,6 +609,50 @@ func brightness(img image.Image, v float64) draw.Image { return brightness } +//圆角 左上 右上 右下 左下 +func borderRadius(img image.Image, lt, rt, rb, lb uint) draw.Image { + b := img.Bounds() + dx := b.Dx() + dy := b.Dy() + + lti := int(lt) + rti := int(rt) + rbi := int(rb) + lbi := int(lb) + border := image.NewRGBA(img.Bounds()) + for x1 := 0; x1 < dx; x1++ { + for y1 := 0; y1 < dy; y1++ { + if x1 < lti && y1 < lti { + if math.Pow(float64(lti-x1), 2)+math.Pow(float64(lti-y1), 2) > math.Pow(float64(lti), 2) { + continue + } + } + + if x1 > dx-rti && y1 < rti { + if math.Pow(float64(rti-dx+x1), 2)+math.Pow(float64(rti-y1), 2) > math.Pow(float64(rti), 2) { + continue + } + } + + if x1 > dx-rbi && y1 > dy-rbi { + if math.Pow(float64(rbi-dx+x1), 2)+math.Pow(float64(rbi-dy+y1), 2) > math.Pow(float64(rbi), 2) { + continue + } + } + + if x1 < lbi && y1 > dy-lbi { + if math.Pow(float64(lbi-x1), 2)+math.Pow(float64(lbi-dy+y1), 2) > math.Pow(float64(lbi), 2) { + continue + } + } + + border.Set(x1, y1, img.At(x1, y1)) + + } + } + return border +} + //从本地读取图片 func LoadImage(path string) (*Image, error) { img, err := loadImage(path) @@ -686,6 +730,11 @@ func (i *Image) Cut(x, y, w, h int) *Image { return NewImage(cut(i.img, x, y, x+w, y+h)) } +//圆角 左上 右上 右下 左下 +func (i *Image) BorderRadius(lt, rt, rb, lb uint) *Image { + return NewImage(borderRadius(i.img, lt, rt, rb, lb)) +} + //调整图片大小并且返回一个新的对象 w宽度 h高度 func (i Image) Resize(w, h int, resizeType ...ResizeType) *Image { if len(resizeType) == 0 {