当前位置: 首页 > news >正文

【uniapp】图片合成并导入base64

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

调用

this.mergeImgs(this.imgList).then((res)=>{console.log(res,'图片base64')
})

方法

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);});},

http://www.mrgr.cn/news/3720.html

相关文章:

  • 基于RDMA技术的Mayastor解决方案
  • Ajax技术详解
  • python 将mysql转为csv、json导入到Doris数据库
  • 英国海外媒体通稿宣发:顶级媒体宣发
  • Java并发编程(19)—— CompletableFuture异步多线程
  • 【从Qwen2,Apple Intelligence Foundation,Gemma 2,Llama 3.1看大模型的性能提升之路】
  • 「Qt Widget中文示例指南」如何实现一个旋转框(二)
  • SQL - 事件
  • 力扣面试经典算法150题:跳跃游戏
  • 【JavaEE初阶】TCP协议
  • linux(arm) 双网口物理地址(phyaddr)相同
  • Python知识点:如何使用Azure SDK for Python进行Azure服务操作
  • 【自用14.6】C++俄罗斯方块-方块降落前的准备
  • Leetcode-day27-贪心算法
  • 【题目/训练】:双指针
  • tomcat相关
  • Manim动画:相机的移动(MovingCameraScene)
  • C语言 | Leetcode C语言题解之第354题俄罗斯套娃信封问题
  • Apache CloudStack Official Document 翻译节选(七)
  • HTML静态网页成品作业(HTML+CSS)——自行车介绍网页设计制作(1个页面)