status-go/images/encode.go

77 lines
1.4 KiB
Go
Raw Normal View History

package images
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/jpeg"
"io"
)
type EncodeConfig struct {
2020-10-27 14:42:42 +00:00
Quality int
}
func Encode(w io.Writer, img image.Image, config EncodeConfig) error {
// Currently a wrapper for renderJpeg, but this function is useful if multiple render formats are needed
return renderJpeg(w, img, config)
}
func renderJpeg(w io.Writer, m image.Image, config EncodeConfig) error {
o := new(jpeg.Options)
o.Quality = config.Quality
return jpeg.Encode(w, m, o)
}
func EncodeToLimits(bb *bytes.Buffer, img image.Image, bounds DimensionLimits) error {
q := MaxJpegQuality
for q > MinJpegQuality-1 {
err := Encode(bb, img, EncodeConfig{Quality: q})
if err != nil {
return err
}
if bounds.Ideal > bb.Len() {
return nil
}
if q == MinJpegQuality {
if bounds.Max > bb.Len() {
return nil
}
2020-11-24 23:16:19 +00:00
return fmt.Errorf(
"image size after processing exceeds max, expect < '%d', received < '%d'",
bounds.Max,
2020-11-24 23:16:19 +00:00
bb.Len(),
)
}
2020-10-22 15:59:01 +00:00
bb.Reset()
q -= 2
}
return nil
}
func EncodeToBestSize(bb *bytes.Buffer, img image.Image, size ResizeDimension) error {
return EncodeToLimits(bb, img, DimensionSizeLimit[size])
}
func GetPayloadDataURI(payload []byte) (string, error) {
if len(payload) == 0 {
return "", nil
}
mt, err := GetMimeType(payload)
if err != nil {
return "", err
}
b64 := base64.StdEncoding.EncodeToString(payload)
return "data:image/" + mt + ";base64," + b64, nil
}