/
image.go
144 lines (120 loc) · 3.06 KB
/
image.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
package gopdf
import (
"fmt"
"github.com/zhyocean/gopdf/core"
"os"
"path/filepath"
"time"
)
type Image struct {
pdf *core.Report
autobreak bool
path string
width, height float64
margin core.Scope
temppath []string
}
func NewImage(path string, pdf *core.Report) *Image {
return NewImageWithWidthAndHeight(path, 0, 0, pdf)
}
func NewImageWithWidthAndHeight(path string, width, height float64, pdf *core.Report) *Image {
contentWidth, contentHeight := pdf.GetContentWidthAndHeight()
if width > contentWidth {
width = contentWidth
}
if height > contentHeight {
height = contentHeight
}
var temppath []string
if _, err := os.Stat(path); err != nil {
path = fmt.Sprintf("/tmp/%v.png", time.Now().Unix())
temppath = append(temppath, path)
DrawPNG(path)
}
dstPath := fmt.Sprintf("/tmp/%v.jpeg", time.Now().UnixNano())
srcPath, _ := filepath.Abs(path)
Convert2JPEG(srcPath, dstPath)
temppath = append(temppath, dstPath)
image := &Image{
pdf: pdf,
path: dstPath,
temppath: temppath,
}
image.imageWidthHeight(image.path, width, height)
if dstPath != "" {
pdf.AddCallBack(image.delTempImage)
}
return image
}
func (image *Image) imageWidthHeight(path string, width, height float64) *Image {
w, h := GetImageWidthAndHeight(path)
if width > 0 && height > 0 {
if float64(h)*width/float64(w) > height {
width = float64(w) * height / float64(h)
} else {
height = float64(h) * width / float64(w)
}
} else if width > 0 {
height = float64(h) * width / float64(w)
} else if height > 0 {
width = float64(w) * height / float64(h)
} else {
width, height = float64(w), float64(h)
}
image.width = width
image.height = height
return image
}
func (image *Image) SetMargin(margin core.Scope) *Image {
margin.ReplaceMarign()
image.margin = margin
return image
}
func (image *Image) GetHeight() float64 {
return image.height
}
func (image *Image) GetWidth() float64 {
return image.width
}
func (image *Image) SetAutoBreak() {
image.autobreak = true
}
// 自动换行
func (image *Image) GenerateAtomicCell() (pagebreak, over bool, err error) {
var (
sx, sy = image.pdf.GetXY()
)
x, y := sx+image.margin.Left, sy+image.margin.Top
pageEndX, pageEndY := image.pdf.GetPageEndXY()
if y < pageEndY && y+float64(image.height) > pageEndY {
if image.autobreak {
image.pdf.AddNewPage(false)
goto draw
}
return true, false, nil
}
draw:
image.pdf.Image(image.path, x, y, x+float64(image.width), y+float64(image.height))
if x+float64(image.width) >= pageEndX {
sx, _ = image.pdf.GetPageStartXY()
image.pdf.SetXY(sx, y+float64(image.height)+image.margin.Bottom)
} else {
image.pdf.SetXY(x+float64(image.width), y)
}
if image.autobreak {
sx, _ = image.pdf.GetPageStartXY()
_, sy := image.pdf.GetXY()
image.pdf.SetXY(sx, sy+image.height)
}
return false, true, nil
}
func (image *Image) delTempImage(report *core.Report) {
if image.temppath == nil {
return
}
for _, path := range image.temppath {
if _, err := os.Stat(path); err == nil || os.IsExist(err) {
os.Remove(path)
}
}
}