Skip to content

Commit 212fd22

Browse files
committed
图片合成:计算图片大小算法优化
1 parent 5edbcfa commit 212fd22

File tree

1 file changed

+35
-64
lines changed

1 file changed

+35
-64
lines changed

src/views/image/ImageSynthesizer.vue

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<template slot="hintInfo">
66
<p>基于VueDRR拖拽功能,在其上通过叠加图片、文字等,实现图片的叠加伪合成功能</p>
77
<p>VueDRR:基于vue-draggable-resizable的vue组件,可以实现拖动、拉伸和旋转功能</p>
8-
<p>github地址:访问 <el-link type="success" href="https://github.com/Alvin-Liu/vue-drr" target="_blank">VueDRR</el-link></p>
98
</template>
109
</Hints>
1110
<el-row :gutter="20">
@@ -73,7 +72,7 @@ export default {
7372
height: 0,
7473
bgImageSrc: '../../../static/img/crop-image.jpg' // 底图路径
7574
},
76-
elements: [],
75+
elements: [], // 叠加组件数组
7776
activeEle: {}, // 当前图片上聚焦的叠加组件
7877
eleNum: 0
7978
}
@@ -127,6 +126,7 @@ export default {
127126
zIndex: zIndex
128127
}
129128
},
129+
// 底图上传成功
130130
handleSuccess (data) {
131131
this.container.bgImageSrc = data
132132
const image = new Image()
@@ -135,39 +135,46 @@ export default {
135135
this.getDragContainerSize(image.width, image.height)
136136
}
137137
},
138-
// 计算图片展示区宽高
139-
getDragContainerSize (width, height) {
140-
let [imageTrueW, imageTrueH, imageShowW, imageShowH] = [+width, +height, 0, 0]
141-
let [imageMaxW, imageMaxH] = [850, 550]
142-
138+
// 计算图片宽高
139+
getImageSize (imageTrueW, imageTrueH, showAreaW, showAreaH) {
140+
let [width, height] = [0, 0]
143141
// 图片真实宽大于真实高
144142
if (imageTrueW > imageTrueH) {
145-
if (imageTrueW >= imageMaxW) { // 真实宽大于或等于最大宽
146-
const imageRatioH = imageTrueH * (imageMaxW / imageTrueW)
147-
// 按最大宽与实际宽比率换算后,高度大于显示高度时
148-
if (imageRatioH >= imageMaxH) {
149-
imageShowW = imageTrueW * (imageMaxH / imageTrueH)
150-
imageShowH = imageMaxH
143+
if (imageTrueW >= showAreaW) { // 真实宽大于或等于展示区最大宽
144+
const imageRatioH = imageTrueH * (showAreaW / imageTrueW)
145+
// 按展示区最大宽与实际宽比率换算后,高度大于显示高度时
146+
if (imageRatioH >= showAreaW) {
147+
width = imageTrueW * (showAreaH / imageTrueH)
148+
height = showAreaH
151149
} else {
152-
imageShowW = imageMaxW
153-
imageShowH = imageRatioH
150+
width = showAreaW
151+
height = imageRatioH
154152
}
155153
} else {
156-
imageShowW = imageTrueW
157-
imageShowH = imageTrueH
154+
width = imageTrueW
155+
height = imageTrueH
158156
}
159-
} else if (imageTrueW <= imageTrueH) { // 图片真实宽小于或等于真实高
160-
if (imageTrueH >= imageMaxH) { // 真实高大于或等于最大高
161-
imageShowW = imageTrueW * (imageMaxH / imageTrueH)
162-
imageShowH = imageMaxH
157+
} else { // 图片真实宽小于或等于真实高
158+
if (imageTrueH >= showAreaH) { // 真实高大于或等于展示区最大高
159+
width = imageTrueW * (showAreaH / imageTrueH)
160+
height = showAreaH
163161
} else {
164-
imageShowW = imageTrueW
165-
imageShowH = imageTrueH
162+
width = imageTrueW
163+
height = imageTrueH
166164
}
167165
}
166+
return {
167+
width,
168+
height
169+
}
170+
},
171+
// 计算底图展示宽高
172+
getDragContainerSize (imageW, imageH) {
173+
const [showAreaW, showAreaH] = [850, 550]
174+
const sizeObj = this.getImageSize(imageW, imageH, showAreaW, showAreaH)
168175
// 更新图片展示区宽高
169-
this.container.width = imageShowW
170-
this.container.height = imageShowH
176+
this.container.width = sizeObj.width
177+
this.container.height = sizeObj.height
171178
},
172179
// 添加文本
173180
addText () {
@@ -229,52 +236,16 @@ export default {
229236
const img = new Image()
230237
img.src = data
231238
img.onload = () => {
232-
let imageSize = this.getImageSize(img.width, img.height, this.container.width, this.container.height)
239+
let imageSize = this.getImageSize(img.width, img.height, parseInt(this.container.width / 4), parseInt(this.container.height / 4))
233240
this.addImage({
234241
active: true,
235242
src: data,
236243
size: dataSize,
237-
width: imageSize.w,
238-
height: imageSize.h
244+
width: imageSize.width,
245+
height: imageSize.height
239246
})
240247
}
241248
},
242-
// 计算叠加图片的宽和高
243-
getImageSize (width, height, showAreaW, showAreaH) {
244-
let [w, h] = [0, 0]
245-
// 添加的叠加图默认最大为底图的1/4
246-
let showW = parseInt(showAreaW / 4)
247-
let showH = parseInt(showAreaH / 4)
248-
// 图片真实宽大于真实高
249-
if (width > height) {
250-
if (width >= showW) { // 真实宽大于或等于最大宽
251-
const imageRatioH = height * (showW / width)
252-
// 按最大宽与实际宽比率换算后,高度大于显示高度时
253-
if (imageRatioH >= showH) {
254-
w = width * (showH / height)
255-
h = showH
256-
} else {
257-
w = showW
258-
h = imageRatioH
259-
}
260-
} else {
261-
w = width
262-
h = height
263-
}
264-
} else if (width <= height) { // 图片真实宽小于或等于真实高
265-
if (height >= showH) { // 真实高大于或等于最大高
266-
w = width * (showH / height)
267-
h = showH
268-
} else {
269-
w = width
270-
h = height
271-
}
272-
}
273-
return {
274-
w,
275-
h
276-
}
277-
},
278249
// 更新当前选中的元素
279250
updateActiveEle (element) {
280251
this.activeEle = element

0 commit comments

Comments
 (0)