flx:png图片转换为webp格式

This commit is contained in:
Rain_ 2025-09-08 09:22:24 +08:00
parent 7becabc202
commit 108854a197
3 changed files with 81 additions and 24 deletions

View File

@ -103,7 +103,7 @@ if (process.env.NODE_ENV === 'development') {
// Vue.prototype.url_config = 'http://8.136.222.164:8808/'//中科安信 // Vue.prototype.url_config = 'http://8.136.222.164:8808/'//中科安信
// Vue.prototype.url_config = 'http://101.43.164.214:11111/'// 百色三标段 // Vue.prototype.url_config = 'http://101.43.164.214:11111/'// 百色三标段
// Vue.prototype.url_config = 'http://jxj.zhgdyun.com:9500/'// 测试环境h5 // Vue.prototype.url_config = 'http://jxj.zhgdyun.com:9500/'// 测试环境h5
Vue.prototype.url_config = 'http://jxj.zhgdyun.com:21000/' // 潮州项目h5 // Vue.prototype.url_config = 'http://jxj.zhgdyun.com:21000/' // 潮州项目h5
} else { } else {
// 生产环境 // 生产环境

View File

@ -414,6 +414,10 @@
import { import {
dateformat dateformat
} from "@/utils/tool.js" } from "@/utils/tool.js"
import {
pathToBase64,
base64ToPath
} from '@/utils/imgPachToBase64/index.js'
export default { export default {
components: { components: {
tkiTree, tkiTree,
@ -1520,7 +1524,14 @@
count: 5 - that.imgFileList.length, count: 5 - that.imgFileList.length,
success(res) { success(res) {
const tempFilePaths = res.tempFilePaths; const tempFilePaths = res.tempFilePaths;
that.canversList(tempFilePaths[0]); // #ifdef H5
that.uploadFileFn(tempFilePaths[0]);
// #endif
// #ifdef APP-PLUS
that.canversList(tempFilePaths[0]);
// #endif
// uni.uploadFile({ // uni.uploadFile({
// url: that.url_config + 'upload/image', // // url: that.url_config + 'upload/image', //
// filePath: tempFilePaths[0], // filePath: tempFilePaths[0],

View File

@ -22,7 +22,7 @@
weatherInfo: '--', // weatherInfo: '--', //
adressDetail: '--', // adressDetail: '--', //
url: '', url: '',
systemInfo: {}, userInfo: {},
} }
}, },
props: ['imgList'], props: ['imgList'],
@ -32,12 +32,7 @@
mounted() { mounted() {
console.log("啦啦啦啦啦", this.imgList) console.log("啦啦啦啦啦", this.imgList)
var that = this; var that = this;
uni.getSystemInfo({ this.userInfo = JSON.parse(uni.getStorageSync('userInfo'));
success(res) {
that.systemInfo = res;
console.log(111222, res)
}
})
this.date = this.dateFormat(new Date().toISOString()) this.date = this.dateFormat(new Date().toISOString())
this.loadWeather(); this.loadWeather();
@ -115,30 +110,29 @@
}) })
let that = this; let that = this;
console.log('我开始画啦 ' + JSON.stringify(that.imgList)) console.log('我开始画啦 ' + JSON.stringify(that.imgList))
console.log(that.systemInfo)
const ctx = uni.createCanvasContext('myCanvas', that); // const ctx = uni.createCanvasContext('myCanvas', that); //
ctx.drawImage(that.imgList.imgUrl, 0, 0, that.imgList.imgWidth, that.imgList ctx.drawImage(that.imgList.imgUrl, 0, 0, that.imgList.imgWidth, that.imgList
.imgHeight); // .imgHeight); //
if(that.imgList.imgWidth > that.imgList.imgHeight){ if (that.imgList.imgWidth > that.imgList.imgHeight) {
ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList
.imgHeight * .imgHeight *
0.72, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.22); // 0.72, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.22); //
ctx.setFontSize(25); // ctx.setFontSize(25); //
} else if(that.imgList.imgHeight / that.imgList.imgWidth < 2) { } else if (that.imgList.imgHeight / that.imgList.imgWidth < 2) {
ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList
.imgHeight * .imgHeight *
0.72, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.2); // 0.72, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.2); //
ctx.setFontSize(25); // ctx.setFontSize(25); //
} else if(that.imgList.imgHeight / that.imgList.imgWidth < 2.2) { } else if (that.imgList.imgHeight / that.imgList.imgWidth < 2.2) {
ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList
.imgHeight * .imgHeight *
0.74, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.16); // 0.74, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.16); //
ctx.setFontSize(25); // ctx.setFontSize(25); //
} else { } else {
ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList ctx.drawImage(require('@/static/CanvarsImg.png'), that.imgList.imgWidth * 0.52, that.imgList
.imgHeight * .imgHeight *
0.74, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.16); // 0.74, that.imgList.imgWidth * 0.5, that.imgList.imgHeight * 0.16); //
ctx.setFontSize(17); // ctx.setFontSize(17); //
} }
ctx.setFillStyle('#FFFFFF'); // ctx.setFillStyle('#FFFFFF'); //
ctx.setFillStyle('#000000'); // ctx.setFillStyle('#000000'); //
@ -150,8 +144,10 @@
// ctx.fillText('' + that.adressDetail, that.imgList.imgWidth * 0.56, that.imgList // ctx.fillText('' + that.adressDetail, that.imgList.imgWidth * 0.56, that.imgList
// .imgHeight * // .imgHeight *
// 0.88); // // 0.88); //
const insertIntervalString = (originStr, disNum = 10, insertStr = "\n") => originStr.replace(new RegExp("(.{" + disNum + "})", "g"), "$1" + insertStr); const insertIntervalString = (originStr, disNum = 10, insertStr = "\n") => originStr.replace(new RegExp(
that.drawMultilineText(ctx, insertIntervalString('地点:' + that.locationInfo, 18), that.imgList.imgWidth * 0.56, that.imgList.imgHeight * "(.{" + disNum + "})", "g"), "$1" + insertStr);
that.drawMultilineText(ctx, insertIntervalString('地点:' + that.locationInfo, 18), that.imgList.imgWidth *
0.56, that.imgList.imgHeight *
0.86, 40); 0.86, 40);
// ctx.fillText('' + that.imgList.shooter, that.imgList.imgWidth * 0.56, that.imgList // ctx.fillText('' + that.imgList.shooter, that.imgList.imgWidth * 0.56, that.imgList
// .imgHeight * // .imgHeight *
@ -196,11 +192,61 @@
let that = this let that = this
pathToBase64(url).then(data => { pathToBase64(url).then(data => {
// console.log('base64', data); //database64 // console.log('base64', data); //database64
that.$emit('imgUrl', { // fetch(
imageBase64: data, // that.url_config +
imgUrl: that.imgUrl, // "xmgl/upload/convertBase642webp", {
// method: "post",
// headers: {
// Authorization: that.userInfo.token,
// "Content-Type": "application/json", // JSON
// },
// body: {
// base64: data,
// },
// }
// )
// .then((response) => {
// //
// if (!response.ok) {
// throw new Error("");
// }
// return response.blob();
// })
// .then((blob) => {
// console.log("", URL.createObjectURL(blob));
// const url = URL.createObjectURL(blob);
// that.$emit('imgUrl', {
// imageBase64: data,
// // imgUrl: that.imgUrl,
// imgUrl: url,
// })
// that.imgList.imgWidth = null
// })
// .catch((error) => {
// //
// console.error(error);
// });
that.sendRequest({
url: 'xmgl/upload/convertBase642webp',
method: 'post',
data: {
base64: data,
},
success: (res) => {
console.log(res);
if(res.code == 200) {
base64ToPath(`data:image/webp;base64,${res.result}`).then(url => {
console.log(222, url)
that.$emit('imgUrl', {
imageBase64: data,
// imgUrl: that.imgUrl,
imgUrl: url,
})
that.imgList.imgWidth = null
})
}
}
}) })
that.imgList.imgWidth = null
}) })
}, },
dateFormat(time) { dateFormat(time) {