Skip to content

Commit

Permalink
feat:Image新增创建副本方法、调整部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
yeyudekuangxiang committed Jul 2, 2021
1 parent 00e7546 commit 82a17fa
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,13 @@ func cut(img image.Image, x, y, x1, y1 int) draw.Image {

//将img image.Image转化为draw.Image
func convertImage(img image.Image) draw.Image {
dst := image.NewRGBA(img.Bounds())
draw.Draw(dst, dst.Bounds(), img, image.Pt(0, 0), draw.Src)
return dst
newImg := image.NewRGBA(img.Bounds())
for x := 0; x <= img.Bounds().Max.X; x++ {
for y := 0; y <= img.Bounds().Max.Y; y++ {
newImg.Set(x, y, img.At(x, y))
}
}
return newImg
}

//将图片保存在本地
Expand Down Expand Up @@ -471,7 +475,8 @@ func (i Image) Resize(w, h int) *Image {

//将其他元素填充进本图片
func (i *Image) Fill(item ...FillItem) *Image {
return NewImage(fill(i.img, item...))
i.img = fill(i.img, item...)
return i
}

//将图片保存在本地
Expand Down Expand Up @@ -523,3 +528,10 @@ func (i *Image) Image() draw.Image {
func (i *Image) Encode(writer io.Writer, ext string) error {
return saveWriter(i.img, ext, writer)
}

//创建一个副本
func (i *Image) Copy() *Image {
copyImg := *i
copyImg.img = convertImage(i.img)
return &copyImg
}

0 comments on commit 82a17fa

Please sign in to comment.