Skip to content

Commit

Permalink
feat:添加圆角功能
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyudekuangxiang committed Nov 5, 2022
1 parent 17f1dd2 commit 4e2e6f3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//色调、饱和度、亮度、不透明度调整
Expand All @@ -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("夜雨寄北(李商隐)君问归期未有期,巴山夜雨涨秋池。何当共剪西窗烛,却话巴山夜雨时。")
Expand Down
49 changes: 49 additions & 0 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4e2e6f3

Please sign in to comment.