小程序生成海报保存分享图片功能怎么实现
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,这篇文章主要介绍"小程序生成海报保存分享图片功能怎么实现",在日常操作中,相信很多人在小程序生成海报保存分享图片功能怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答
千家信息网最后更新 2025年11月14日小程序生成海报保存分享图片功能怎么实现
这篇文章主要介绍"小程序生成海报保存分享图片功能怎么实现",在日常操作中,相信很多人在小程序生成海报保存分享图片功能怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"小程序生成海报保存分享图片功能怎么实现"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

业务
在小程序中生成海报(包括用户头像和自定义文字)并且保存到本地
实现思路
利用canvas画布,把用户头像和自定义文字定位好,用户点击按钮保存到本地
注意事项 难点
小程序canvas不支持自定义宽高,反正我没找到,canvas画布大部分业务都需要全屏,响应式,至少宽100%
解决方案:判断到屏幕尺寸,传到wxml 里面
远程图片不能直接使用 getImageInfo 获取,需要保存到本地
解决方案:canvas直接支持远程图片,不需要使用这个api
技术栈
canvas
wx.createCanvasContext
wx.canvasToTempFilePath
Promise
实战
首先我们在wxml里面写一个canvas占位
注意这里的宽度是100%,响应式,海报的高posterHeight 是从js里面动态计算的
根据屏幕动态计算海报的尺寸
data: { motto: 'Hello World', hidden: true, userInfo: {}, hasUserInfo: false, windowWidth: '', posterHeight: '',},onLoad: function () { const poster = { "with": 375, "height": 587 } const systemInfo = wx.getSystemInfoSync() let windowWidth = systemInfo.windowWidth let windowHeight = systemInfo.windowHeight let posterHeight = parseInt((windowWidth / poster.with) * poster.height) this.setData({ windowWidth: windowWidth, posterHeight: posterHeight })}背景图片生成
const that = this // 图片路径 const imagePath = '../../static/image/common/' let bgimgPromise = new Promise(function (resolve, reject) { console.log('data', that.data) wx.getImageInfo({ src: imagePath + "base.png", success: function (res) { resolve(res); } }) });头像直接使用远程头像
初始化的时候,调取,一定在生成海报之前
此处可以存储本地,或使用状态都可以
wxml
// 可以从后端接口获取 或 官方本身远程地址
js
getUserInfo: function (e) { app.globalData.userInfo = e.detail.userInfo let userInfo = e.detail.userInfo console.log('userInfo', userInfo) // 更新用户信息 // api.post('更新用户信息的url', userInfo) this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) },生成海报背景和图片
wxml
bgimgPromise.then(res => { console.log('Promise.all', res) const ctx = wx.createCanvasContext('shareImg') ctx.width = windowWidth ctx.height = posterHeight console.log(windowWidth, posterHeight) // 背景图 ctx.drawImage('../../' + res[0].path, 0, 0, windowWidth, posterHeight, 0, 0) // 头像 ctx.drawImage(that.data.userInfo.avatarUrl, 48, 182, 58, 58, 0, 0) ctx.setTextAlign('center') ctx.setFillStyle('#000') ctx.setFontSize(22) // ctx.fillText('分享文字2:stark.wang出品', 88, 414) ctx.fillText('分享文字1我的博客:https://shudong.wang', 55, 414) ctx.stroke() ctx.draw() })保存到本地
onLoad: function () { share: function () { var that = this wx.showLoading({ title: '正在制作海报。。。' }) new Promise(function (resolve, reject) { wx.canvasToTempFilePath({ x: 0, y: 0, width: 444, height: 500, destWidth: 555, destHeight: 666, canvasId: 'starkImg', success: function (res) { console.log(res.tempFilePath); that.setData({ prurl: res.tempFilePath, hidden: false }) wx.hideLoading() resolve(res) }, fail: function (res) { console.log(res) } }) }).then(res => { console.log(res) this.save() }) }}更新头像裁剪为圆形
ctx.save() // 对当前区域保存ctx.beginPath() // 开始新的区域ctx.arc(73, 224, 38, 0, 2 * Math.PI);ctx.clip(); // 从画布上裁剪出这个圆形ctx.drawImage(res[1], 36, 186, 94, 94, 0, 0) // 把图片填充进裁剪的圆形ctx.restore() // 恢复
上面是远程连接容易发生请求失败
把头像提前存到本地存储中解决
getImg: function () { let avatarUrl = this.data.userInfo.avatarUrl downLoadFile(avatarUrl).then((res) => { console.log(res) wx.saveFile({ tempFilePath: res.data.tempFilePath, success: function (res) { wx.setStorageSync('avatarUrl', res.savedFilePath) } }) })},获取头像
// 头像let promiseAvatarUrl = new Promise(function (resolve, reject) { resolve(wx.getStorageSync('avatarUrl'))}).catch(res=>{ console.log('catch',res)});背景还是不变
const that = thislet promiseBdImg = new Promise(function (resolve, reject) { console.log('data', that.data) wx.getImageInfo({ src: imagePath + "base1.png", success: function (res) { console.log('promiseBdImg', res) resolve(res); } })此时生成canvas更新
Promise.all([ promiseBdImg, promiseAvatarUrl ]).then(res => { console.log('Promise.all', res) const ctx = wx.createCanvasContext('shareImg') ctx.width = windowWidth ctx.height = posterHeight console.log(windowWidth, posterHeight) //主要就是计算好各个图文的位置 ctx.drawImage('../../' + res[0].path, 0, 0, windowWidth, posterHeight, 0, 0) ctx.save() // 对当前区域保存 ctx.beginPath() // 开始新的区域 ctx.arc(73, 224, 38, 0, 2 * Math.PI); ctx.clip(); // 从画布上裁剪出这个圆形 ctx.drawImage(res[1], 36, 186, 94, 94, 0, 0) // 把图片填充进裁剪的圆形 ctx.restore() // 恢复 ctx.setTextAlign('center') ctx.setFillStyle('#000') ctx.setFontSize(22) ctx.save() ctx.beginPath(); ctx.fillText('作者:stark.wang', 545 / 2, 130) ctx.fillText('我的博客:http://shudong.wang', 190, 414) ctx.stroke() ctx.draw() })到此,关于"小程序生成海报保存分享图片功能怎么实现"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
图片
海报
生成
头像
程序
用户
圆形
功能
区域
文字
画布
背景
学习
更新
信息
业务
动态
博客
尺寸
屏幕
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网公司数据库选择
美的软件开发招聘
软件开发的经营范围
十八大以来网络安全法治成果丰硕
小米5空间管理服务器
网络安全服务行业分析
数据库往表中导入信息
网络安全等级保护领导小组
mysql数据库实战
我的世界最良心的斗罗服务器
宝塔怎么访问数据库
柔性网络技术的作用
服务器动态ip地址
华为手机软件开发人选项怎么找
联系 数据库
用网络技术加快信息化的发展
网络安全管理措施包括哪几种
手机网络安全管理责任书
软件开发速度和质量
网络安全创新产品有哪些
苹果app 数据库
知网数据库检索方式
湖南简单的项目管控软件开发平台
光学制图软件开发
波场软件开发
龙岩市网络安全支撑单位
武尊gm数据库
北京新中工软件开发中心
南京字节流网络技术有限公司
软件开发合同纠纷案件