2021-02-26 02:15:18 +13:00
|
|
|
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)
|
2021-02-26 05:16:01 +13:00
|
|
|
err := jpeg.Encode(out, img, &jpeg.Options{Quality: quality})
|
2021-02-26 02:15:18 +13:00
|
|
|
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
|
|
|
|
}
|