-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
62 lines (53 loc) · 1.59 KB
/
util.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
package sqip
import (
"encoding/base64"
"fmt"
"image"
_ "image/gif" // register gif
_ "image/jpeg" // register jpeg
_ "image/png" // register png
"io"
"io/ioutil"
"os"
)
// LoadImage takes a path and returns the content as an image.
func LoadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return decodeImage(file)
}
func decodeImage(r io.Reader) (image.Image, error) {
im, _, err := image.Decode(r)
return im, err
}
// SaveFile creates the named file with mode 0666 (before umask), truncating
// it if it already exists, and writes the content to it.
func SaveFile(path, content string) error {
return ioutil.WriteFile(path, []byte(content), 0666)
}
// StringBase64 is a base64 encoded string.
type StringBase64 string
// Base64 returns the base64 encoding of src.
func Base64(src string) StringBase64 {
enc := base64.StdEncoding.EncodeToString([]byte(src))
return StringBase64(enc)
}
// ImageTag creates an example <img> tag.
func ImageTag(filename string, svg StringBase64, width, height int) string {
return fmt.Sprintf(`<img width="%d" height="%d" src="%s" alt="Add descriptive alt text" style="background-size: cover; background-image: url(data:image/svg+xml;base64,%s);">`, width, height, filename, svg)
}
// ImageWidthAndHeight returns the width and height of input image.
func ImageWidthAndHeight(input image.Image) (width, height int) {
bounds := input.Bounds()
return bounds.Dx(), bounds.Dy()
}
// largerOne returns the larger int of two ints.
func largerOne(x, y int) int {
if x > y {
return x
}
return y
}