微信小程序 自定义图片分享-绘制数据图片以及信息文字

一 、需求

从数据库中读取头像,姓名电话等信息,当分享给女朋友时,每个信息不一样

二、实现方案

1、先将数据库中需要的头像姓名信息读取出来加载到data 数据项中

复制代码
data:{
     firstName:'', // 姓名
      img:'', // 头像
     shareImage:'',// 存储临时图片
}

2、当进入页面时,就产生图片,分享时直接分享 在小程序onLoad函数中实现

css 复制代码
    // 生成分享图片
    generateShareImage() {

        // 进入页面就下载头像到临时地址中
        const imgUrl = app.globalData.base_url +'uploads/lawyer/'+this.data.img
        wx.downloadFile({
            url: imgUrl,
            success: (res) => {
              if (res.statusCode === 200) {

                // 下载成功,获取临时路径
                const tempFilePath = res.tempFilePath;
                console.log("666"+tempFilePath)

                // 开始绘制 注意这里
                this.drawPoster(tempFilePath);
              }
            },
            fail: (err) => {
              console.error('图片下载失败:', err);
            }
        });
    },

    // 绘制海报
    drawPoster(avatarPath){
        // createCanvasContext 绘制方法
        const ctx = wx.createCanvasContext('shareCanvas');

        // 绘制背景图(可以是本地或网络路径)
        const bgImagePath = '/images/sharebg.jpg'; // 替换为你的背景图地址

        // 1. 绘制背景图
        ctx.drawImage(bgImagePath, 0, 0, this.data.canvasWidth, this.data.canvasHeight);

        // 2. 设置字体样式
        ctx.setFontSize(10);
        ctx.setFillStyle('black');

        // 3. 绘制姓名头像
        if(avatarPath){
            ctx.save()
            ctx.arc(40, 30, 11 * 2, 0, 2 * Math.PI) // 圆形边框
            //ctx.strokeStyle = '#1A1A1A' // 设置绘制圆形边框的颜色
            ctx.stroke() // 绘制出圆形,默认为黑色,可通过 ctx.strokeStyle = '#FFFFFF',设置想要的颜色
            ctx.clip()
            ctx.drawImage(avatarPath,  8, 6, 70, 70) // 图像大小
            ctx.restore()
        }

        // ctx.fillText(`${this.data.firstName}`, 100, 50);
        drawMultiLineText(ctx,`${this.data.firstName}`,80,28,50, 14, 14);

       
        drawMultiLineText(ctx,`${this.data.title}`,80,45,50, 14, 10);

       
        // ctx.fillText(`${this.data.mobilePhoneNumber}`, 10, 90);
        drawMultiLineText(ctx,`${this.data.mobilePhoneNumber}`,35,70,100, 14, 10);

        
        // ctx.fillText(`${this.data.email}`,10,110);
        drawMultiLineText(ctx,`${this.data.email}`,35,90,110, 14, 10);

       
        drawMultiLineText(ctx,`${this.data.address_details}`,35,109,90, 16, 10);

        // 5. 绘制完成
        ctx.draw(false, () => {

          // 6. 将 Canvas 导出为临时图片路径
          wx.canvasToTempFilePath({

            canvasId: 'shareCanvas',
            success: (res) => {
              // res.tempFilePath 是生成的临时图片路径
              console.log(this.data.mobilePhoneNumber)
              this.setData({
                shareImage: res.tempFilePath
              });
              // 触发分享
              this.onShareAppMessage();
            },
            fail: (err) => {
              wx.showToast({ title: '生成图片失败', icon: 'none' });
            }
          });
        });
    },
css 复制代码
onLoad(options){
     // 调用绘制方法
    this.generateShareImage()
}

3、分享图片

onShareAppMessage 触发分享

css 复制代码
 onShareAppMessage() {
        return {
            title:'标题',//标题
            path: '/pages/index/index',//路径
            imageUrl: this.data.shareImage//图片
        }
    }
相关推荐
头发还在的女程序员20 小时前
基于JAVA语言的短剧小程序-抖音短剧小程序
java·开发语言·小程序
2501_916007471 天前
iOS 应用性能测试的工程化流程,构建从指标采集到问题归因的多工具协同测试体系
android·ios·小程序·https·uni-app·iphone·webview
源码_V_saaskw1 天前
JAVA国际版同城跑腿源码快递代取帮买帮送同城服务源码支持Android+IOS+H5
android·java·ios·微信小程序
tbit1 天前
fluwx 拉起小程序WXLog:Error:fail to load Keychain status:-25300, keyData null:1
flutter·ios·微信小程序
book多得1 天前
刷题专用微信小程序推荐
微信小程序·小程序
00后程序员张1 天前
iOS 抓不到包怎么办?从 HTTPS 解密、QUIC 排查到 TCP 数据流分析的完整解决方案
android·tcp/ip·ios·小程序·https·uni-app·iphone
技术与健康1 天前
微信小程序云开发实践:共享环境与LLM整合经验
微信小程序·小程序
老华带你飞1 天前
社区养老保障|智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·社区养老保障
发财北1 天前
本地生活小程序开发方案
小程序
游戏开发爱好者82 天前
iOS 商店上架全流程解析 从工程准备到审核通过的系统化实践指南
android·macos·ios·小程序·uni-app·cocoa·iphone