mirror of
https://github.com/status-im/status-go.git
synced 2025-01-18 10:42:07 +00:00
a6e7ff6ddd
* Sound check * Add DecodeImageURL fn * Introduce crop dynamics * Add center crop calculations png * Apply suggestions from code review Co-authored-by: RichΛrd <info@richardramos.me> * Fix lint * Rebase and update version Co-authored-by: RichΛrd <info@richardramos.me>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package images
|
|
|
|
import (
|
|
"bytes"
|
|
"image"
|
|
)
|
|
|
|
func GenerateImageVariants(cImg image.Image) ([]*IdentityImage, error) {
|
|
var iis []*IdentityImage
|
|
var err error
|
|
|
|
for _, s := range ResizeDimensions {
|
|
rImg := Resize(s, cImg)
|
|
|
|
bb := bytes.NewBuffer([]byte{})
|
|
err = EncodeToBestSize(bb, rImg, s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ii := &IdentityImage{
|
|
Name: ResizeDimensionToName[s],
|
|
Payload: bb.Bytes(),
|
|
Width: rImg.Bounds().Dx(),
|
|
Height: rImg.Bounds().Dy(),
|
|
FileSize: bb.Len(),
|
|
ResizeTarget: int(s),
|
|
}
|
|
|
|
iis = append(iis, ii)
|
|
}
|
|
|
|
return iis, nil
|
|
}
|
|
|
|
func GenerateIdentityImages(filepath string, aX, aY, bX, bY int) ([]*IdentityImage, error) {
|
|
img, err := Decode(filepath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cropRect := image.Rectangle{
|
|
Min: image.Point{X: aX, Y: aY},
|
|
Max: image.Point{X: bX, Y: bY},
|
|
}
|
|
cImg, err := Crop(img, cropRect)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return GenerateImageVariants(cImg)
|
|
}
|
|
|
|
func GenerateIdentityImagesFromURL(url string) ([]*IdentityImage, error) {
|
|
img, err := DecodeFromURL(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cImg, err := CropCenter(img)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return GenerateImageVariants(cImg)
|
|
}
|