2020-09-29 16:09:42 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
2020-10-08 20:24:41 +00:00
|
|
|
"fmt"
|
2020-09-29 16:09:42 +00:00
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/nfnt/resize"
|
|
|
|
"github.com/oliamb/cutter"
|
2020-12-09 16:51:32 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-09-29 16:09:42 +00:00
|
|
|
)
|
|
|
|
|
2020-10-22 15:59:01 +00:00
|
|
|
func Resize(size ResizeDimension, img image.Image) image.Image {
|
2020-10-09 20:42:26 +00:00
|
|
|
var width, height uint
|
|
|
|
|
2020-10-27 14:42:42 +00:00
|
|
|
switch {
|
2020-10-09 20:42:26 +00:00
|
|
|
case img.Bounds().Max.X == img.Bounds().Max.Y:
|
2020-10-22 15:59:01 +00:00
|
|
|
width, height = uint(size), uint(size)
|
2020-10-09 20:42:26 +00:00
|
|
|
case img.Bounds().Max.X > img.Bounds().Max.Y:
|
2020-10-22 15:59:01 +00:00
|
|
|
width, height = 0, uint(size)
|
2020-10-09 20:42:26 +00:00
|
|
|
default:
|
2020-10-22 15:59:01 +00:00
|
|
|
width, height = uint(size), 0
|
2020-10-09 20:42:26 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 16:51:32 +00:00
|
|
|
log.Info("resizing", "size", size, "width", width, "height", height, "img", img)
|
|
|
|
|
2020-10-08 20:24:41 +00:00
|
|
|
return resize.Resize(width, height, img, resize.Bilinear)
|
2020-09-29 16:09:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 20:24:41 +00:00
|
|
|
func Crop(img image.Image, rect image.Rectangle) (image.Image, error) {
|
|
|
|
|
2020-10-27 14:42:42 +00:00
|
|
|
if img.Bounds().Max.X < rect.Max.X || img.Bounds().Max.Y < rect.Max.Y {
|
2020-10-08 20:24:41 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"crop dimensions out of bounds of image, image width '%dpx' & height '%dpx'; crop bottom right coordinate at X '%dpx' Y '%dpx'",
|
|
|
|
img.Bounds().Max.X, img.Bounds().Max.Y,
|
|
|
|
rect.Max.X, rect.Max.Y,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:09:42 +00:00
|
|
|
return cutter.Crop(img, cutter.Config{
|
2020-10-08 20:24:41 +00:00
|
|
|
Width: rect.Dx(),
|
|
|
|
Height: rect.Dy(),
|
|
|
|
Anchor: rect.Min,
|
2020-09-29 16:09:42 +00:00
|
|
|
})
|
|
|
|
}
|