1、在分享的page 添加 canvas 标签

<canvas canvas-id="canvas"style="position: absolute; top: -1000px; left: -1000px;width: 225px; height: 180px;"></canvas>

2、在分享的page 引入makeCanvas(之后下边会定义),并在onShareAppMessage里使用

onShareAppMessage(res) {         let shareMessage = {title: '标题',path: `要分享的page 路径`,imageUrl: '图片url'}return new Promise((resolve, reject) => {uni.showLoading({title: '请求分享数据',icon: 'none'})
makeCanvas(shareMessage.imageUrl).then(imgPath => {uni.hideLoading()resolve({title: shareMessage.title,path: shareMessage.path,imageUrl: imgPath})}).catch(err => {uni.hideLoading()resolve({title: shareMessage.title,path: shareMessage.path,imageUrl:'处理失败后展示的图片,可以用原图shareMessage.imageUrl'})})})}

3、图片处理的主要文件(导出makeCanvas),一般作为工具包,需要的时候引入
这里将图片处理为5:4的大小,默认生成的图片为jpg格式

/*** 生成分享5:4的图片*/
const makeCanvas = (imgUrl) => {return new Promise((resolve, reject) => {// 获取图片信息,小程序下获取网络图片信息需先配置download域名白名单才能生效uni.getImageInfo({src: imgUrl,success: (imgInfo) => {let ctx = uni.createCanvasContext('canvas')let canvasW = 0let canvasH = imgInfo.height// 把比例设置为 宽比高 5:4canvasW = (imgInfo.height * 5) / 4// 为画框设置背景色,注意要放在画图前,图会覆盖在背景色上ctx.fillStyle = "#fff";if (imgInfo.width > 225 || imgInfo.height > 180) {canvasW = 225;canvasH = 180;ctx.fillRect(0, 0, canvasW, canvasH);let dWidth = canvasW / imgInfo.width; // canvas与图片的宽度比例let dHeight = canvasH / imgInfo.height; // canvas与图片的高度比例let dWH = imgInfo.width / imgInfo.height; //宽高比if (imgInfo.width > canvasW && imgInfo.height > canvasH) {// console.log(dWH);if (dWH > 1 && dWH < 1.5) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width * dHeight) / 2,0, imgInfo.width * dHeight, imgInfo.height *dHeight)} else {if (imgInfo.width > imgInfo.height) {ctx.drawImage(imgInfo.path, 0, (canvasH - imgInfo.height *dWidth) / 2, imgInfo.width * dWidth,imgInfo.height *dWidth)}if (imgInfo.width == imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width *dHeight) / 2, 0, imgInfo.width * dHeight,imgInfo.height * dHeight)}if (imgInfo.width < imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width *dHeight) / 2, 0, imgInfo.width * dHeight,imgInfo.height * dHeight)}}} else {if (imgInfo.width > imgInfo.height) {ctx.drawImage(imgInfo.path, 0, (canvasH - imgInfo.height) / 2,imgInfo.width * dWidth, imgInfo.height)}if (imgInfo.width == imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width * dHeight) / 2,0, imgInfo.width * dHeight, imgInfo.height *dHeight)}if (imgInfo.width < imgInfo.height) {ctx.drawImage(imgInfo.path, (canvasW - imgInfo.width * dHeight) / 2,0, imgInfo.width * dHeight, imgInfo.height *dHeight)}}} else {ctx.fillRect(0, 0, canvasW, canvasH)ctx.drawImage(imgInfo.path,0,0,canvasW,canvasH,(canvasW - imgInfo.width) / 2, // 宽度从中间向两边填充0,canvasW,canvasH)}ctx.draw(false, () => {uni.canvasToTempFilePath({width: canvasW,height: canvasH,destWidth: 750, // 标准的iphone6尺寸的两倍,生成高清图destHeight: 600,canvasId: "canvas",fileType: "jpg", // 注意jpg默认背景为透明success: (res) => {resolve(res.tempFilePath)},fail: (err) => {reject(err)}})})},fail: (err) => {reject(err)}})})
}
module.exports = {makeCanvas,
}

之后在微信小程序分享时应该就能看到效果了

微信小程序 分享图片大小处理相关推荐

  1. 微信小程序分享图片给微信好友(如二维码)

    目录 前言 一.使用哪种api? 二.对图片的处理需要转化为临时路径 三.Windows端兼容性问题 前言 最近在公司开发一个微信小程序项目,用到的uniapp技术,在分享图片时,查看了uniapp的 ...

  2. 微信小程序分享图片给微信好友

    wx.showShareImageMenu(Object object) | 微信开放文档 <button class="share-item" bindtap=" ...

  3. 微信小程序裁剪图片大小(有效果图)

    效果图 .wxml <button bindtap="uploadtap">上传图片</button> <image style="widt ...

  4. 微信小程序分享图片给朋友

    //分享share() {returnuni.showLoading({title: '加载中'});wx.downloadFile({url: this.$http.getHost() + this ...

  5. 详解 - 解决微信小程序分享功能图片比例问题 - 全局分享

    前言: 我在我的博客小程序使用微信小程序分享功能 图片不符合5:4问题 ,对其原理 扫描下面二维码,可以体验哦 准备 在需要自定义分享的页面 设置canvas 组件 目录 准备 详解思路 定义总函数 ...

  6. 微信小程序分享功能imageUrl带随机分享图片

    //此为微信小程序分享 onShareAppMessage: function(options) { //先写一个数组, var shareimg =[ "https://desk-fd.z ...

  7. 微信小程序分享接口卡片图片尺寸比例

    调用微信小程序分享需要button标签 <button open-type="share">test</button> 然后在js的page里加一个函数 o ...

  8. 微信小程序实现图片是上传、预览功能

    本文实例讲述了微信小程序实现图片上传.删除和预览功能的方法,分享给大家供大家参考,具体如下: 这里主要介绍一下微信小程序的图片上传图片删除和图片预览 1.可以调用相机也可以从本地相册选择 2.本地实现 ...

  9. 微信小程序分享朋友圈功能

    微信小程序分享到朋友圈的功能,终于实现了!但是只能是点击右上角菜单方式分享!话不多说 直接上代码! js Page({onLoad: function (options) {if(options.in ...

最新文章

  1. Linux DHCP服务搭建
  2. [转]linux 下自动登录到ftp
  3. 【错误记录】Android Studio 编译报错 ( VirtualApp 编译 NDK 报错 | Error:A problem occurred configuring project ‘: )
  4. Paging of Large Resultsets in ASP.NET中介绍的SET ROWCOUNT方式存储过程的问题
  5. ajax nginx 转发 sessionid_Nginx+Apache实现动静分离 - 孙天飞
  6. SAP标准OK Code Values 列表
  7. 动态规划问题之一马当先
  8. 【读书笔记】《深入浅出nodejs》第五章 内存控制
  9. [loss]Triphard loss优雅的写法
  10. 一加8系列有望明年二季度发布:配备双曲面打孔屏
  11. 网络层-IP地址和MAC地址
  12. 程序员吐槽的“面试造火箭、工作拧螺丝”,用应聘司机的场景还原当下奇葩的面试
  13. 怎么用计算机求原函数,科学计算器使用教程_科学计算器怎么解方程
  14. Harry Potter and the Goblet of Fire
  15. java 怎么暂停程序_java – 如何暂停程序直到按下按钮?
  16. mysql workbench中PK,NN,UQ,BIN,UN,ZF,AI字段类型标识说明
  17. 【面试招聘】 科班小硕的2020年面试小结
  18. 带头像的用户显示功能
  19. [转载]高一语文必修一寒假作业二
  20. SAP携手“大数据之都” 共推大数据创新

热门文章

  1. Nova内部组件详解
  2. 河南一高校副校长美出圈,被称校领导颜值天花板
  3. 读书笔记之《高效程序员的45个习惯----敏捷开发之道》 摘录
  4. 率土之滨九游不显示服务器,【公告】《率土之滨》关于“不攻”调整说明
  5. Android图片选择框架
  6. 系统重装后OA服务器,华为服务器重装系统
  7. SpringBoot在东方通服务器TongWeb上显示pdf
  8. 卸载并重装eclipse
  9. python 新闻_Python一行代码竟能干这种事,同学都欢呼:要向女神表白!
  10. 贵州:“区块链+”促经济转型产业升级