优化图片上传

This commit is contained in:
haomingming 2025-09-17 12:10:59 +08:00
parent 4076beccde
commit a67deea0f1
5 changed files with 43 additions and 9 deletions

View File

@ -2263,8 +2263,22 @@ Page({
})
const {imgSrc,name}=e.detail
console.log(e);
// this.getFileSize(imgSrc)
this.uploadImg(imgSrc,name)
},
// 获取临时文件大小
getFileSize(filePath) {
wx.getFileInfo({
filePath: filePath,
success: (res) => {
console.log('文件大小:', res.size/1024);
},
fail: (err) => {
console.error('获取文件信息失败:', err);
wx.showToast({ title: '获取文件大小失败', icon: 'none' });
}
});
},
afterRead(event) {
console.log( event.detail)
const { file,name} = event.detail;

View File

@ -310,7 +310,7 @@ Component({
height: Math.round(this.data.height * this.data.export_scale),
destWidth: this.data.width * this.data.export_scale,
destHeight: Math.round(this.data.height) * this.data.export_scale,
fileType: 'png',
fileType: 'jpg',
quality: this.data.quality,
canvasId: this.data.el,
success: (res) => {
@ -849,7 +849,7 @@ Component({
height: Math.round(this.data.height * this.data.export_scale),
destWidth: this.data.width * this.data.export_scale,
destHeight: Math.round(this.data.height) * this.data.export_scale,
fileType: 'png',
fileType: 'jpg',
quality: this.data.quality,
canvasId: this.data.el,
success: (res) => {

View File

@ -2,7 +2,6 @@
<van-overlay show="{{showPaint}}" z-index="99999999" >
<view class="upage" style="position:reltive;background-color: #000;height:100vh;" catch:tap="hideBarsHandler" wx:if="{{showPaint}}" >
<canvas canvas-id="myCanvas" disable-scroll="true" bindtouchstart="touchStart"
bindtouchmove="touchMove" bindtouchend="touchEnd" wx:if="{{hasChoosedImg}}"
style="position:relative;margin:0 auto;height: {{(canvasHeightLen == 0) ? canvasHeight : canvasHeightLen}}px; width: {{canvasWidth}}px; background: url('{{background}}');background-position: 0 0; background-size: {{canvasWidth}}px {{canvasHeight}}px"

View File

@ -26,7 +26,8 @@
"disablePlugins": [],
"outputPath": ""
},
"packNpmManually": true
"packNpmManually": true,
"condition": false
},
"condition": {},
"editorSetting": {

View File

@ -44,7 +44,7 @@ function formatTime(date) {
// 点击按钮触发的事件
function tapBtn(e, _this, pageType,from,isSmall=false) {
let btnType = e.target.dataset.type;
console.log("btnType: ", btnType)
let c = {};
switch (btnType) {
@ -157,14 +157,21 @@ function formatTime(date) {
success: function (res) {
// 把单纯用户画的内容存好了
let src = res.tempFilePath;
// getFileSize(src)
// getFileSize(_this.data.background)
let ctx = wx.createCanvasContext('myCanvas',_this);
// console.log("tempCanvas:", ctx)
// console.log("_this.data.canvasWidth: ", _this.data.canvasWidth)
// console.log("_this.data.canvasHeight: ", _this.data.canvasHeight)
// 照片
ctx.drawImage(_this.data.background, 0, 0, _this.data.canvasWidth, _this.data.canvasHeight);
// 覆盖上画的内容
ctx.drawImage(src, 0, 0, _this.data.canvasWidth, _this.data.canvasHeight);
//let that=this
ctx.draw(false,function(){
_canvaseSaveToImg(_this,from);
_canvaseSaveToImg(_this,from,isSmall);
});
@ -172,11 +179,24 @@ function formatTime(date) {
}
},_this);
} else {
_canvaseSaveToImg(_this,from);
_canvaseSaveToImg(_this,from,isSmall);
}
// }
}
// 获取临时文件大小
function getFileSize(filePath) {
wx.getFileInfo({
filePath: filePath,
success: (res) => {
console.log('文件信息:', res);
console.log('文件大小:', res.size/1024);
},
fail: (err) => {
console.error('获取文件信息失败:', err);
wx.showToast({ title: '获取文件大小失败', icon: 'none' });
}
});
}
function _canvaseSaveToImg(_this,from,isSmall) {
let that=_this;
console.log(from)
@ -186,7 +206,7 @@ function formatTime(date) {
setTimeout(()=>{
wx.canvasToTempFilePath({
canvasId: 'myCanvas',
fileType:'png',
fileType:'jpg',
quality:isSmall?0.8:1,
success: function (res) {
// wx.previewImage({