2020-10-08 15:30:18 +00:00
|
|
|
package images
|
2020-10-22 15:27:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"image"
|
|
|
|
)
|
|
|
|
|
2020-10-27 15:14:00 +00:00
|
|
|
func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]*IdentityImage, error) {
|
2020-10-22 15:27:58 +00:00
|
|
|
img, err := Decode(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cropRect := image.Rectangle{
|
2020-10-27 14:42:42 +00:00
|
|
|
Min: image.Point{X: aX, Y: aY},
|
|
|
|
Max: image.Point{X: bX, Y: bY},
|
2020-10-22 15:27:58 +00:00
|
|
|
}
|
|
|
|
cImg, err := Crop(img, cropRect)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:14:00 +00:00
|
|
|
iis := make([]*IdentityImage, len(ResizeDimensions))
|
2020-10-22 15:27:58 +00:00
|
|
|
for _, s := range ResizeDimensions {
|
|
|
|
rImg := Resize(s, cImg)
|
|
|
|
|
|
|
|
bb := bytes.NewBuffer([]byte{})
|
|
|
|
err = EncodeToBestSize(bb, rImg, s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-27 15:14:00 +00:00
|
|
|
|
|
|
|
ii := &IdentityImage{
|
2020-10-27 15:41:20 +00:00
|
|
|
Name: ResizeDimensionToName[s],
|
2020-10-27 15:14:00 +00:00
|
|
|
Payload: bb.Bytes(),
|
|
|
|
Width: rImg.Bounds().Dx(),
|
|
|
|
Height: rImg.Bounds().Dy(),
|
|
|
|
FileSize: bb.Len(),
|
|
|
|
ResizeTarget: int(s),
|
|
|
|
}
|
|
|
|
|
|
|
|
iis = append(iis, ii)
|
2020-10-22 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 15:14:00 +00:00
|
|
|
return iis, nil
|
2020-10-27 14:42:42 +00:00
|
|
|
}
|