mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
40 lines
718 B
Go
40 lines
718 B
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"image"
|
|
"image/jpeg"
|
|
"image/png"
|
|
)
|
|
|
|
func CreatePNGImage(img *image.RGBA) ([]byte, error) {
|
|
out := new(bytes.Buffer)
|
|
err := png.Encode(out, img)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return out.Bytes(), nil
|
|
}
|
|
|
|
func CreateJPGImage(img *image.RGBA, quality int) ([]byte, error) {
|
|
out := new(bytes.Buffer)
|
|
err := jpeg.Encode(out, img, &jpeg.Options{ Quality: quality })
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return out.Bytes(), nil
|
|
}
|
|
|
|
func CreatePNGImageURI(img *image.RGBA) (string, error) {
|
|
data, err := CreatePNGImage(img)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
uri := "data:image/png;base64," + base64.StdEncoding.EncodeToString(data)
|
|
return uri, nil
|
|
}
|