-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added healthchecks, edited image generation logic
- Loading branch information
marcussss1
committed
Jun 7, 2023
1 parent
6391bcb
commit 0ed510f
Showing
3 changed files
with
114 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,80 @@ | ||
package image_generation | ||
|
||
import ( | ||
"crypto/rand" | ||
"github.com/fogleman/gg" | ||
"github.com/labstack/gommon/log" | ||
"image" | ||
"image/color" | ||
"image/draw" | ||
"image/png" | ||
"math" | ||
"math/big" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func GenerateAvatar(firstCharacterName string) error { | ||
//firstCharacterName = strings.ToUpper(firstCharacterName) | ||
//img := image.NewRGBA(image.Rect(0, 0, 1024, 1024)) | ||
// | ||
//rBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32)) | ||
//if err != nil { | ||
// return err | ||
//} | ||
// | ||
//gBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32)) | ||
//if err != nil { | ||
// return err | ||
//} | ||
// | ||
//bBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32)) | ||
//if err != nil { | ||
// return err | ||
//} | ||
// | ||
//color := color.RGBA{uint8(rBig.Uint64()), uint8(gBig.Uint64()), uint8(bBig.Uint64()), 255} | ||
// | ||
//draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.Point{}, draw.Src) | ||
// | ||
//file, err := os.Create("background.png") | ||
//if err != nil { | ||
// return err | ||
//} | ||
//defer func() { | ||
// err = file.Close() | ||
// if err != nil { | ||
// log.Error(err) | ||
// } | ||
//}() | ||
// | ||
//if err := png.Encode(file, img); err != nil { | ||
// return err | ||
//} | ||
// | ||
//const S = 1024 | ||
//im, err := gg.LoadImage("background.png") | ||
//if err != nil { | ||
// return err | ||
//} | ||
// | ||
//dc := gg.NewContext(S, S) | ||
//dc.SetRGB(1, 1, 1) | ||
//dc.Clear() | ||
//dc.SetRGB(1, 1, 1) | ||
//if err := dc.LoadFontFace("str.ttf", 728); err != nil { | ||
// return err | ||
//} | ||
// | ||
//dc.DrawRoundedRectangle(0, 0, 512, 512, 0) | ||
//dc.DrawImage(im, 0, 0) | ||
//dc.DrawStringAnchored(firstCharacterName, S/2, S/2, 0.5, 0.5) | ||
//dc.Clip() | ||
// | ||
//err = dc.SavePNG("background.png") | ||
//if err != nil { | ||
// return err | ||
//} | ||
firstCharacterName = strings.ToUpper(firstCharacterName) | ||
img := image.NewRGBA(image.Rect(0, 0, 1024, 1024)) | ||
|
||
rBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
gBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bBig, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
color := color.RGBA{uint8(rBig.Uint64()), uint8(gBig.Uint64()), uint8(bBig.Uint64()), 255} | ||
|
||
draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.Point{}, draw.Src) | ||
|
||
file, err := os.Create("background.png") | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
err = file.Close() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}() | ||
|
||
if err := png.Encode(file, img); err != nil { | ||
return err | ||
} | ||
|
||
const S = 1024 | ||
im, err := gg.LoadImage("background.png") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
dc := gg.NewContext(S, S) | ||
dc.SetRGB(1, 1, 1) | ||
dc.Clear() | ||
dc.SetRGB(1, 1, 1) | ||
if err := dc.LoadFontFace("str.ttf", 728); err != nil { | ||
return err | ||
} | ||
|
||
dc.DrawRoundedRectangle(0, 0, 512, 512, 0) | ||
dc.DrawImage(im, 0, 0) | ||
dc.DrawStringAnchored(firstCharacterName, S/2, S/2, 0.5, 0.5) | ||
dc.Clip() | ||
|
||
err = dc.SavePNG("background.png") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |