【uniapp】图片合成并导入base64

两张图片合成,宽度固定,高度根据图片自适应

调用

javascript 复制代码
this.mergeImgs(this.imgList).then((res)=>{
	console.log(res,'图片base64')
})

方法

javascript 复制代码
mergeImgs(imgList) {
				// 图片合成
				return new Promise((resolve, reject) => {
					Promise.all(this.fileDtoList.map(imgList)).then((images) => {
						const canvas = document.createElement('canvas')
						const ctx = canvas.getContext('2d')
						// 确定合成图片的宽度
						const maxWidth = 1000; // 设置期望的宽度
						// 计算合成图片的高度
						const totalHeight = images.reduce((total, img) => {
						  const aspectRatio = img.width / img.height;
						  const scaledHeight = maxWidth / aspectRatio;
						  return total + scaledHeight;
						}, 0);
				
						// 设置 canvas 的大小
						canvas.width = maxWidth;
						canvas.height = totalHeight;
				
						// 在 canvas 上绘制图片
						let currentY = 0;
						images.forEach(image => {
						  const aspectRatio = image.width / image.height;
						  const scaledHeight = maxWidth / aspectRatio;
						  ctx.drawImage(image, 0, currentY, maxWidth, scaledHeight);
						  currentY += scaledHeight;
						});
						const base64 = canvas.toDataURL('image/png')
						resolve(base64)
					})
				})
			},
			loadImage(url) {
				return new Promise((resolve, reject) => {
					const image = new Image();
					image.onerror = reject;
					image.src = url;
					image.crossOrigin = 'Anonymous'
					image.onload = () => resolve(image);
				});
			},
相关推荐
xy34535 分钟前
Axure 9.0 中继器创建与设置步骤
前端·ui·html·axure·原型·产品设计
FEF前端团队5 分钟前
01# Vibe Coding工作流设计思维:前端开发者的协作框架
前端·cursor·vibecoding
计算机魔术师13 分钟前
OpenAI 发布模型错位报告框架,披露未发布模型自行修改自身指令案例
前端
特创数字科技16 分钟前
公文Word一键批量排版工具|批量统一公文标准格式,办公自动化桌面小工具
前端·python
流水白开21 分钟前
JavaScript的原型链、类与类的继承
前端·javascript
PedroQue9924 分钟前
修复 .uvue 页面路径残留问题
前端·vite
颜进强32 分钟前
从零搭一套 WorkBuddy Skill 骨架:调接口 → 生成 HTML 报告的分层设计与流转拆解
前端·后端·ai编程
YHL37 分钟前
🔍 结构化输出:从 `JSON.parse` 到 `withStructuredOutput` 的三级火箭
前端·深度学习
YHL39 分钟前
🚰流式输出:把 LLM 的每个字,像水一样送到浏览器
前端·javascript