微信小程序 分享图片大小处理

2023-11-13

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 = 0
				let canvasH = imgInfo.height
				// 把比例设置为 宽比高 5:4
				canvasW = (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,
}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

微信小程序 分享图片大小处理 的相关文章

随机推荐

  • jdbc大数据量时处理速度的比较

    在预编译空间够的情况下 使用预编译 addBatch gt 纯预编译 gt 原生sql语句
  • vue移动端无法使用string.replaceAll,报错显示空白

    开发vue时 使用了replaceAll函数 在pc端调试没问题 但是打包部署到移动端端测试时 发现部分页面显示空白 控制台只显示Error 经过排查是replaceAll函数报错了 替换成replace解决
  • 如何将彩图.png转换成灰度图.png(python)

    1 安装所需要的包 使用PIL库需要先安装Pillow包 Pillow是PIL库的一个Fork 分支 它提供了与PIL兼容的API 并进行了一些功能扩展和改进 因此 在使用PIL之前 需要确保已经正确安装了Pillow包 可以使用pip命令
  • Python实现照片右上角添加红色数字

    Python实现照片右上角添加红色数字 在许多图像处理应用程序中 我们经常需要在图像上添加一些标记或注释 本文将介绍如何使用Python编程语言在照片的右上角添加一个红色的数字 我们将使用Python的Pillow库来处理图像 并利用该库提
  • android.intent.extra,Android Intent的几种用法详细解析

    Intent应该算是Android中特有的东西 你可以在Intent中指定程序要执行的动作 比如 view edit dial 以及程序执行到该动作时所需要的资料 都指定好后 只要调用startActivity Android系统会自动寻找
  • C语言预处理条件语句的 与或运算

    C语言预处理条件语句的 与或运算 1 ifdef 与或运算 ifdef MIN MAX 错误使用 if defined MIN defined MAX 正确使用 ifdef MIN MAX 错误使用 if defined MIN defin
  • 汇编语言 第3版 王爽 检测点答案及详细解析

    第一章 基础知识 检测点1 1 1 1个CPU的寻址能力为8KB 那么它的地址总线的宽度为 13位 2 1KB的存储器有 1024 个存储单元 存储单元的编号从 0 到 1023 3 1KB的存储器可以存储 8192 2 13 个bit 1
  • HTML 一文读懂

    目录 1 认识HTML 2 HTML 网页构成 HTML基本结构 网页头部信息 3 HTML 基本标签 4 图像标签 5 链接标签 6 行内元素和块元素 7 列表 8 表格 9 媒体元素 10 页面结构分析 11 HTML 内联框架 ifr
  • 本地搭建web服务器、个人博客并发布公网访问

    文章目录 前言 1 安装套件软件 2 创建网页运行环境 指定网页输出的端口号 3 让WordPress在所需环境中安装并运行 生成网页 4 装修 个人网站 5 将位于本地电脑上的网页发布到公共互联网上 前言 在现代社会 网络已经成为我们生活
  • Spring Boot + Vue的网上商城之登陆认证

    Spring Boot Vue的网上商城之登陆认证 本篇博客介绍了如何使用Spring Boot和Vue来实现网上商城的登陆认证功能 下面是本文的主要内容总结 后端实现 创建Spring Boot项目 并添加Spring Security和
  • 为什么spring单例模式可以支持多线程并发访问

    为什么spring单例模式可以支持多线程并发访问 1 spring单例模式是指 在内存中只实例化一个类的对象 2 类的变量有线程安全的问题 就是有get和set方法的类成员属性 执行单例对象的方法不会有线程安全的问题 因为方法是磁盘上的一段
  • Vulnhub靶机-BLACKLIGHT

    项目地址 http download vulnhub com blacklight BLACKLIGHT ova 靶机渗透 网络选择桥接模式 使用命令 arp scan l nmap 192 168 0 130 使用dirb遍历网站目录结构
  • Linux自学笔记

    Linux自学笔记 06 常用命令 文件目录类 Linux自学笔记 01 文件系统和目录结构 Linux自学笔记 02 VIM编辑器的安装与使用 Linux自学笔记 03 Linux网络配置 Linux自学笔记 04 远程登录 Linux自
  • 高速模数转换器(ADC)的INL/DNL测量

    摘要 尽管积分非线性和微分非线性不是高速 高动态性能数据转换器最重要的参数 但在高分辨率成像应用中却具有重要意义 本文简要回顾了这两个参数的定义 并给出了两种不同但常用的测量高速模数转换器 ADC 的INL DNL的方法 近期 许多厂商推出
  • 微信小程序 ---- 【invalid credential, access_token is invalid】

    报错返回 errcode 40001 errmsg invalid credential access token is invalid or not latest rid 6004f3da 1529ba72 5c345f67 报错原因 a
  • oled拼接屏有哪些安装方法?

    嘉峪关是一个历史悠久的城市 也是一个旅游胜地 为了更好地展示城市的文化和旅游资源 嘉峪关市政府决定在市区的重要场所安装oled拼接屏 oled拼接屏是一种高清晰度的显示屏 具有高亮度 高对比度 高色彩饱和度等优点 它可以将图像和视频以更清晰
  • qtp的基本使用方法(1)

    1 action qtp为每一个action生成相应的测试文件和目录 对象库也是和action绑定的 用action 来划分和组织测试流程 编辑action 修改action的名字 action properties 增加action in
  • StartSSL CA证书签名 和 Tomcat Https访问 全过程说明

    第1章 准备工作 IP地址 外网服务器的IP 如X X 47 xx 作用 1 解析域名 2 部署Tomcat7 域 名 将域名 如samuscasting cn 解析 映射到外网IP 注 1 记住购买域名所使用的邮箱 原因 认证机构对域名做
  • QGIS编译(跨平台编译)之五十一:MacOS环境下安装Python、pyqt5、pyqt5-tools等

    目录 1 安装背景 2 卸载Python 3 下载Python3 9 4 安装Python3 9 5 安装pyqt5 6 安装pyqt5 tools
  • 微信小程序 分享图片大小处理

    1 在分享的page 添加 canvas 标签