-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpact.go
164 lines (134 loc) · 3.55 KB
/
Impact.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package Impact
import (
"bufio"
"fmt"
"image"
"strings"
"image/draw"
"image/jpeg"
"image/png"
"io/ioutil"
"log"
"os"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
func Impact(imagePath string, fontSize float64, toptext, bottomtext string) string {
fontBytes, err := ioutil.ReadFile("resources/impact.ttf")
if err != nil {
panic(err)
}
f, err := freetype.ParseFont(fontBytes)
if err != nil {
panic(err)
}
file, err := os.Open(imagePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
var img_bg image.Image
switch imagePath[len(imagePath)-4:] {
case ".png":
img_bg, err = png.Decode(file)
case ".jpg":
img_bg, err = jpeg.Decode(file)
}
if err != nil {
log.Fatal(err)
}
rgba := image.NewRGBA(img_bg.Bounds())
draw.Draw(rgba, rgba.Bounds(), img_bg, image.Point{}, draw.Src)
c := freetype.NewContext()
py := img_bg.Bounds().Dy() / 100
c.SetFont(f)
c.SetFontSize(fontSize)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetHinting(font.HintingFull)
face := truetype.NewFace(f, &truetype.Options{Size: fontSize})
toptextArr := strings.Split(toptext, " ")
toplines := []string{toptextArr[0]}
toptextArr = toptextArr[1:]
topline := 0
toplenght := 0
for _, v := range toptextArr {
if toplenght+font.MeasureString(face, v).Round() < img_bg.Bounds().Dx() {
toplines[topline] += " " + v
toplenght = font.MeasureString(face, toplines[topline]).Round()
} else {
topline += 1
toplenght = 0
toplines = append(toplines, v)
}
}
n := 3 // stroke
for i, v := range toplines {
textWidth := font.MeasureString(face, v).Round()
c.SetSrc(image.Black)
for dx := -n; dx <= n; dx++ {
for dy := -n; dy < n; dy++ {
if (dx*dx+dy*dy >= n*n) || ((dx == 0) && (dy == 0)) {
continue
}
c.DrawString(v, freetype.Pt(img_bg.Bounds().Dx()/2-textWidth/2+(dx*2), int(fontSize)*(i+1)+(dy*2)))
}
}
c.SetSrc(image.White)
c.DrawString(v, freetype.Pt(img_bg.Bounds().Dx()/2-textWidth/2, int(fontSize)*(i+1)))
}
bottomtextArr := strings.Split(bottomtext, " ")
bottomlines := []string{bottomtextArr[0]}
bottomtextArr = bottomtextArr[1:]
bottomline := 0
bottomlenght := 0
for _, v := range bottomtextArr {
if bottomlenght+font.MeasureString(face, v).Round() < img_bg.Bounds().Dx() {
bottomlines[bottomline] += " " + v
bottomlenght = font.MeasureString(face, bottomlines[bottomline]).Round()
} else {
bottomline += 1
bottomlenght = 0
bottomlines = append(bottomlines, v)
}
}
for i, v := range bottomlines {
textWidth := font.MeasureString(face, v).Round()
c.SetSrc(image.Black)
for dx := -n; dx <= n; dx++ {
for dy := -n; dy < n; dy++ {
if (dx*dx+dy*dy >= n*n) || ((dx == 0) && (dy == 0)) {
continue
}
c.DrawString(v, freetype.Pt(img_bg.Bounds().Dx()/2-textWidth/2+(dx*2), img_bg.Bounds().Dy()-(int(fontSize)*(len(bottomlines)-(i+1))+(dy*2)+ +py*2)))
}
}
c.SetSrc(image.White)
c.DrawString(v, freetype.Pt(img_bg.Bounds().Dx()/2-textWidth/2, img_bg.Bounds().Dy()-(int(fontSize)*(len(bottomlines)-(i+1))+py*2)))
}
a := strings.Split(imagePath, "/")
outFile, err := os.Create(a[0] + "/" + "impact_" + a[1])
if err != nil {
log.Println(err)
}
defer outFile.Close()
fmt.Println(1)
b := bufio.NewWriter(outFile)
switch imagePath[len(imagePath)-4:] {
case ".png":
err = png.Encode(b, rgba)
case ".jpg":
err = jpeg.Encode(b, rgba, &jpeg.Options{Quality: 100})
}
fmt.Println(2)
if err != nil {
panic(err)
}
fmt.Println(2)
err = b.Flush()
if err != nil {
panic(err)
}
return a[0] + "/" + "impact_" + a[1]
}