-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (95 loc) · 2.8 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"image"
"fmt"
"github.com/fogleman/gg"
"net/http"
"log"
"github.com/disintegration/imaging"
"encoding/base64"
"image/png"
"bytes"
"os"
)
func LoadImageByUrl(url string) (img image.Image, ext string, err error) {
ret, err := http.Get(url)
if err != nil {
log.Println(url)
status := map[string]string{}
status["status"] = "400"
status["url"] = url
panic(status)
}
return image.Decode(ret.Body)
}
func GeneratorForPostcard(imageUrl, text, logPath, themLogo, fontFamily string) *gg.Context {
im, _, err := LoadImageByUrl(imageUrl)
cx := gg.NewContext(976, 1456)
cx.SetRGB(1, 1, 1)
cx.Clear()
im = imaging.Resize(im, 830, 830, imaging.NearestNeighbor)// 重制图片的大小
cx.DrawImageAnchored(im, 976 / 2, 1456 / 3, 0.5, 0.5)
im2, err := gg.LoadImage(logPath) // 明信片上的logo
if err != nil {
panic(err)
}
cx.DrawImageAnchored(im2, 976 / 2, 940, 2.7, 0.2)
im3, err := gg.LoadImage(themLogo) // 主题文字
if err != nil {
panic(err)
}
im3 = imaging.Resize(im3, 266, 0, imaging.NearestNeighbor)
cx.DrawImageAnchored(im3, 976 / 2 + 60, 1380, 0.8, 0.2)
cx.SetRGB(0, 0, 0)
if err := cx.LoadFontFace(fontFamily, 24); err != nil {
panic(err)
}
x := float64(976 - 100)
y := 1000
yi := 0
ci := 0
for _, v := range text {
if ci % 9 == 0 {
x = float64(x - 30)
y = 1000
yi = 0
}
cx.DrawStringWrapped(fmt.Sprintf("%c", v), x, float64(y + (yi * 40)), 1, 1, 16, 24, gg.AlignLeft)
yi++
ci++
}
return cx
}
//ToSaveImageForHtml to convert base64 from image.Image
func ToSaveImageForHtml(pathAndName string, ctx *gg.Context) (imagePath string) {
// ctx.SavePNG(pathAndName) // Just only need one code if you want to save image
buf := new(bytes.Buffer)
err := png.Encode(buf, ctx.Image())
img_bytes := buf.Bytes()
if err != nil {
panic(err)
}
base64Str := base64.StdEncoding.EncodeToString(img_bytes)
f, err := os.Create(pathAndName)
if err != nil {
panic(err)
}
_, err = f.WriteString("<html><body><img src=\"data:image/png;base64," + base64Str + "\" /></body></html>")
defer f.Close()
if err != nil {
panic(err)
}
imagePath=pathAndName
return imagePath
}
func main() {
imageUrl := "http://img.tusoapp.com/bb923c29-0d95-449a-a29c-1eaae1ec07ef.jpg"
text := "123天生丽质难自弃,456天生丽质难自弃!天生丽质难自弃,天生丽质难自弃!天生丽质难自弃,天生丽质难自弃!天生丽质难自弃,天生丽质难自弃!233!"
logPath := "./logo.png"
themLogo := "./transformers.png"
fontFamily := "/Library/Fonts/Arial Unicode.ttf"
ctx:=GeneratorForPostcard(imageUrl, text, logPath, themLogo, fontFamily) // to make postcard by url
saveName:="./index-base-64-test.html"
pathName:=ToSaveImageForHtml(saveName,ctx) // save image file
fmt.Println("--->",pathName)
}