draw.js
4.71 KB
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
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-05-19 13:46:20
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-08-30 18:11:11
* @FilePath: \scrm-micro-home\src\mixins\gpt.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import apis from '@/api'
import Drawing from '@/utils/drawing'
const WIDTH = 244
const RATIO = WIDTH / 375
export default {
data() {
return {}
},
methods: {
//获取图片宽度高度
getImgInfo(src) {
return new Promise(resolve => {
if (!src) {
resolve({ dw: 0, dh: 0, src })
}
const img = new Image()
img.src = src
img.onload = () => {
resolve({ dw: img.width, dh: img.height, src })
}
})
},
//主图、内容图、logo合并图片
async createBig(info, logoInfo, titleStyle, subTitleStyle) {
console.log(info)
this.loading = true
const { backgroundImage, merchantLogo, marketingTitle, marketingSubtitle } = info
if (!backgroundImage) return
try {
const res = await Promise.all([this.getImgInfo(backgroundImage), this.getImgInfo(merchantLogo)])
const h = (res[0].dh / res[0].dw) * WIDTH
this.logoInfo = { url: merchantLogo, ...logoInfo }
const drawfun = new Drawing(document.getElementById('canvasId'), {
width: WIDTH,
height: h
}),
imageList = [
{
url: backgroundImage,
x: 0,
y: 0,
width: WIDTH,
height: h
}
]
if (info.merchantLogo) {
imageList.push({ ...logoInfo, url: info.merchantLogo })
}
const styles = [titleStyle, subTitleStyle]
for (const style of styles) {
style.x = parseInt(style.x * RATIO)
style.y = parseInt(style.y * RATIO)
style.size = parseInt(style.size * RATIO)
style.width = parseInt(style.width * RATIO)
}
let textList = [],
title = {
titleStyle,
subTitleStyle
}
if (title) {
marketingTitle.split(' ').forEach((item, index) => {
textList.push({
x: title.titleStyle.x,
y: title.titleStyle.y + index * title.titleStyle.fontSize * 1.14,
fontSize: title.titleStyle.fontSize,
color: title.titleStyle.color,
text: item,
bold: title.titleStyle.bold
})
})
new Array(marketingSubtitle).forEach((item, index) => {
textList.push({
x: title.subTitleStyle.x,
y: title.subTitleStyle.y + title.titleStyle.fontSize * 1.14 * (textList.length - 1),
fontSize: title.subTitleStyle.fontSize,
color: title.subTitleStyle.color,
text: item
})
})
}
console.log(imageList, 'imageList')
const canvasBase64 = await drawfun.begin({
imageList,
textList
})
return new Promise(async resolve => {
resolve(await this.uploadImg(canvasBase64))
})
} catch (e) {
console.log(e)
}
},
async createMain(info, draggableList) {
const style1 = draggableList[0]
const style2 = draggableList[1]
const drawfun = new Drawing(document.getElementById('canvasId'), {
width: WIDTH,
height: info.backgroundHeight
})
const imageList = [
{
url: info.backgroundImage,
x: 0,
y: 0,
width: WIDTH,
height: info.backgroundHeight
}
]
if (info.merchantLogo) {
imageList.push({ ...info.logoInfo, url: info.merchantLogo })
}
const textList = [
{ x: style1.x, y: style1.y, fontSize: style1.size, width: style1.width, text: style1.content, color: style1.color, font: style1.font, shadow: style1.shadow },
{ x: style2.x, y: style2.y, fontSize: style2.size, width: style2.width, text: style2.content, color: style2.color, font: style2.font, shadow: style2.shadow }
]
console.log(imageList, textList)
const canvasBase64 = await drawfun.begin({
imageList,
textList
})
return new Promise(async resolve => {
resolve(await this.uploadImg(canvasBase64))
})
},
uploadImg(imgFile) {
return new Promise((resolve, reject) => {
const base64 = 'png' + '@' + imgFile.split(',')[1]
apis.common.pictureUpload({ file: base64, fileType: 'image' }).then(res => {
resolve(res)
})
})
}
},
mounted() {}
}