image-resizer/images/manipulation.go

52 lines
855 B
Go
Raw Normal View History

package images
import (
"image"
"image/draw"
"log"
"github.com/nfnt/resize"
"github.com/oliamb/cutter"
)
2020-09-18 15:22:10 +00:00
func ResizeSquare(size uint, img image.Image) image.Image {
return resize.Resize(size, 0, img, resize.Bilinear)
}
func Crop(img image.Image) image.Image {
var sl int
if img.Bounds().Max.X > img.Bounds().Max.Y {
sl = img.Bounds().Max.Y
} else {
sl = img.Bounds().Max.X
}
croppedImg, err := cutter.Crop(img, cutter.Config{
2020-09-18 15:22:10 +00:00
Width: sl,
Height: sl,
2020-09-18 15:22:10 +00:00
Mode: cutter.Centered,
})
if err != nil {
log.Fatal(err)
}
return croppedImg
}
func CropCircle(img image.Image, size int) image.Image {
dst := image.NewRGBA(image.Rect(0, 0, size, size))
draw.DrawMask(
dst,
dst.Bounds(),
img,
image.Point{},
&circle{
image.Point{X: size / 2, Y: size / 2},
size / 2,
},
image.Point{},
draw.Over,
)
return dst
}