-
Notifications
You must be signed in to change notification settings - Fork 244
/
encode.go
76 lines (60 loc) · 1.43 KB
/
encode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package images
import (
"bytes"
"encoding/base64"
"fmt"
"image"
"image/jpeg"
"io"
)
type EncodeConfig struct {
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
}
return fmt.Errorf(
"image size after processing exceeds max, expect < '%d', received < '%d'",
bounds.Max,
bb.Len(),
)
}
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
}